In the R language, there are three types of operators that can extract subsets of objects:
? "[" Typically returns an object of the same type as the original object; It can also return multiple elements in an object
? [[] Extracts an object from a list or data frame (data.frame), or extracts a single element from a list or data frame, and the type of the returned object may not be a list or data frame.
? "$" can extract elements from lists and data frames by name, if only from the angle of the extracted elements, and "[[" No Difference
(1) Vector
Vectors can use "[]" to extract individual elements of the corresponding position, or multiple elements, or to set conditions in "[]" to extract the corresponding elements.
> x <-C ("a","B","C","D","E","F") > x[2]# #单个提取[1]"B"> x[2:5]# #批量提取[1]"B" "C" "D" "E"> x[x>"C"]# #条件提取[1]"D" "E" "F"> u<-x>"B" # #逻辑判断> u[1]FALSE FALSE TRUE TRUE TRUE TRUE> X[u]# #逻辑提取[1]"C" "D" "E" "F"
(2) matrix
You can also extract the corresponding element in the matrix, which is returned by default as a cell vector, if the attribute Drop=false (the drop default is True), a 1x1 matrix is returned, and the entire row and whole column elements can be returned by missing values:
>x<- Matrix(1: 6, 2, 3) >x[2,3][1]6>x[up][1]3 # #返回单个元素 >x[1,2,drop=false] [, 1][1,]3 # #返回1 X1 Matrix >x[1,][1]1 3 5 # #返回第一行 and quantify it >x[1,,drop=false] [, 1] [, 2] [, 3]# #返回第一行的矩阵形式[1,]1 3 5
(3) List
> X<-list (one=1:4, two=5) > x$one[1]1 2 3 4$two [1]5> x[1] # #返回名称与值, type list$one[1]1 2 3 4> x[[1]]# #返回元素的值, type is the type of the corresponding position element, not the list type [1]1 2 3 4> x[["One"]]# #也可使用名称索引对应位置的元素 [1]1 2 3 4> x$one[1]1 2 3 4> x[[2]][[1]]# #可以返回特定位置的单个元素 [1]5> x[[C (2,1)]][1]5> X=list (data=1:5) > X[["Data"]][1]1 2 3 4 5> x[["D"]]Null> x[["D", Exact=false]]# #list默认元素名称全局匹配, but the element name can be locally matched when property exact is set to true [1]1 2 3 4 5
(4) Missing value removal
> x<-c(1,2,3,NA,5,NA)> bad<-is.na(x)> bad[1FALSEFALSEFALSE TRUEFALSE TRUE> x[!bad][11235
You can also use the complete.cases () function to determine the missing values.
> x <- c(12NA4NA5)> good<-complete.cases(x)> good[1] TRUE TRUEFALSE TRUEFALSE TRUE> x[good][11245
Complete.cases () can also handle matrix data
> airquality[1:6,] Ozone SOLAR.R Wind TempMonth Day1 A the 7.4 the 5 12 $ 118 8.0 the 5 23 A 149 12.6 About 5 34 - 313 11.5 + 5 45Na Na14.3 About 5 56 -NA14.9 the 5 6> Airquality[good, [1:6,] Ozone SOLAR.R Wind TempMonth Day1 A the 7.4 the 5 12 $ 118 8.0 the 5 23 A 149 12.6 About 5 34 - 313 11.5 + 5 47 at 299 8.6 $ 5 78 + About 13.8 - 5 8
Johns Hopkins University series of data Science courses--r language: extracting subsets