2020-12-18 Datacamp Introduction to R language

用subset()从dataframe里面选中符合条件的数据
subset(my_df, subset = some_condition)

# Select planets with diameter < 1
subset(planets_df,diameter<1)

排序:order 直接用order排序后会显示出原始数据排名
a <- c(100, 10, 1000)
order(a)
[1] 2 1 3

用a[order(a)]则会输出排序后的数据
a[order(a)]
[1] 10 100 1000

Call order() on planets_df$diameter (the diameter column of planets_df). Store the result as positions.
Now reshuffle planets_df with the positions vector as row indexes inside square brackets. Keep all columns. Simply print out the result.

# Use order() to create positions
positions <-  order(planets_df$diameter)

# Use positions to sort planets_df
planets_df[positions,]

该命令就是要按照diameter的顺序建立一个order,然后让整个表格都按order进行排序

然后进入list的部分
Vectors (one dimensional array): can hold numeric, character or logical values. The elements in a vector all have the same data type.

Matrices (two dimensional array): can hold numeric, character or logical values. The elements in a matrix all have the same data type.

Data frames (two-dimensional objects): can hold numeric, character or logical values. Within a column all elements have the same data type, but different columns can be of different data type.

白送了200XP,现在要建立一个list要包含vector,matrix, dataframe

# Vector with numerics from 1 up to 10
my_vector <- 1:10 

# Matrix with numerics from 1 up to 9
my_matrix <- matrix(1:9, ncol = 3)

# First 10 elements of the built-in data frame mtcars
my_df <- mtcars[1:10,]

# Construct list with these different elements:
my_list <-list(my_vector,my_matrix,my_df)

现在是要给list 里的东西命名

# Vector with numerics from 1 up to 10
my_vector <- 1:10 

# Matrix with numerics from 1 up to 9
my_matrix <- matrix(1:9, ncol = 3)

# First 10 elements of the built-in data frame mtcars
my_df <- mtcars[1:10,]

# Adapt list() call to give the components names
my_list <- list(vec = my_vector, mat = my_matrix, df = my_df)

# Print out my_list
my_list

这里要注意一个地方,比较麻烦的命名方法:

my_list <- list(your_comp1, your_comp2)
names(my_list) <- c("name1", "name2")

题目给的solution是简便的

my_list <- list(name1 = your_comp1, 
                name2 = your_comp2)

选中List里的东西:
A quick way to check this out is typing it in the console. Important to remember: to select elements from vectors, you use single square brackets: [ ]. Don't mix them up!

You can also refer to the names of the components, with [[ ]] or with the $ sign. Both will select the data frame representing the reviews:

shining_list[["reviews"]]
shining_list$reviews

Besides selecting components, you often need to select specific elements out of these components. For example, with shining_list[[2]][1] you select from the second component, actors (shining_list[[2]]), the first element ([1]). When you type this in the console, you will see the answer is Jack Nicholson.

下面是要用到综合知识了
要建一个dataframe,然而我忘了
好了dataframe是这么建的

> C1 <-c(1,2,3,4)
> C2 <-c(5,6,7,8)
> C3 <-c(9,10,11,12)
> C4 <-c(13,14,15,16)
> C5 <-c(17,18,19,20)
> mydataframe <- data.frame(C1,C2,C3,C4,C5,row.names = c("R1","R2","R3","R4"))
> mydataframe
   C1 C2 C3 C4 C5
R1  1  5  9 13 17
R2  2  6 10 14 18
R3  3  7 11 15 19
R4  4  8 12 16 20

题目:Create two vectors, called scores and comments, that contain the information from the reviews shown in the table.
Find the average of the scores vector and save it as avg_review.
Combine the scores and comments vectors into a data frame called reviews_df.
Create a list, called departed_list, that contains the movie_title, movie_actors, reviews data frame as reviews_df, and the average review score as avg_review, and print it out.
源代码:

# Use the table from the exercise to define the comments and scores vectors
scores <- c(4.6, 5, 4.8, 5, 4.2)
comments <- c("I would watch it again", "Amazing!", "I liked it", "One of the best movies", "Fascinating plot")

# Save the average of the scores vector as avg_review  
avg_review<-mean(scores)
# Combine scores and comments into the reviews_df data frame
reviews_df<-data.frame(scores,comments)

# Create and print out a list, called departed_list
departed_list<-list(movie_title,movie_actors,reviews_df,avg_review)

然后结束了,我划水了好几天才看完。前面笔记得补补,不然全都记不得,

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • the term factor refers to statistical data type used to h...
    无机牛奶阅读 333评论 0 0
  • 久违的晴天,家长会。 家长大会开好到教室时,离放学已经没多少时间了。班主任说已经安排了三个家长分享经验。 放学铃声...
    飘雪儿5阅读 7,550评论 16 22
  • 今天感恩节哎,感谢一直在我身边的亲朋好友。感恩相遇!感恩不离不弃。 中午开了第一次的党会,身份的转变要...
    迷月闪星情阅读 10,602评论 0 11
  • 可爱进取,孤独成精。努力飞翔,天堂翱翔。战争美好,孤独进取。胆大飞翔,成就辉煌。努力进取,遥望,和谐家园。可爱游走...
    赵原野阅读 2,760评论 1 1
  • 在妖界我有个名头叫胡百晓,无论是何事,只要找到胡百晓即可有解决的办法。因为是只狐狸大家以讹传讹叫我“倾城百晓”,...
    猫九0110阅读 3,311评论 7 3