swirl 6: Subsetting Vectors

来源:互联网 发布:php网页聊天室原理 编辑:程序博客网 时间:2024/06/05 08:05
> x[x>0]
 [1]          NA 1.212753086 1.226772040 1.859602576 0.763847244          NA 1.791336311          NA          NA
[10]          NA          NA          NA          NA          NA 0.439779205 2.324976502          NA          NA
[19]          NA          NA          NA 0.009213532          NA 0.141221402          NA 0.524453811          NA
[28]          NA          NA 0.922737419          NA

Since NA is not a value, but rather a placeholder for an unknown quantity, the expression NA > 0 evaluates to NA. Hence we get a bunch of NAs mixed in with our positive numbers when we do this.

> x[-c(2,10)]
 [1]           NA  1.212753086  1.226772040 -0.332124446  1.859602576  0.763847244           NA  1.791336311
 [9]           NA           NA           NA           NA -0.262890059           NA           NA -0.539133551
[17]           NA -0.940835275 -0.396005495 -0.556808402  0.439779205  2.324976502           NA           NA
[25]           NA           NA           NA  0.009213532           NA  0.141221402           NA  0.524453811
[33] -0.207030473           NA           NA           NA  0.922737419           NA

“-”标示去除

> names(vect)
[1] "foo"  "bar"  "norf"

names()

> identical(names(vect),names(vect2))
[1] TRUE
identical() The identical() function tells us if its first two arguments are, well, identical.

> vect["bar"]
bar 
  2 

"name of vector"向量中元素名,(加引号)可以直接返回元素名元素值

0 0
原创粉丝点击