用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)
然后结束了,我划水了好几天才看完。前面笔记得补补,不然全都记不得,