0

数値要素と NA 要素を持つベクトルがあります。例えば、

data<-c(.4, -1, 1, NA, 8, NA, -.4)
data[complete.cases(data), ]

しかし、箱ひげ図や ECDF などのグラフを使用してそれらを比較できるように、それらを異なるベクトルに分離する機能は何ですか?

4

1 に答える 1

4

It's not clear what problem you are trying to solve. complete.cases creates a logical vector for selection (if you use it correctly.) You can negate it to get the other ones. You cannot address a vector as you attempted with [ , ] but if 'data' were a dataframe (or a matrix) that would have worked.

data<-c(.4, -1, 1, NA, 8, NA, -.4)
data[complete.cases(data) ]
#[1]  0.4 -1.0  1.0  8.0 -0.4
data[!complete.cases(data) ]
#[1] NA NA

If one is trying to select the non-NA items it might be easier to use !is.na(data) as the selection vector. This is a test case showing it works with matrices as well as data.frames:

> dat <- matrix( sample(c(1,2,NA), 12, rep=TRUE), 3)
> dat
     [,1] [,2] [,3] [,4]
[1,]    1    1    1    1
[2,]   NA   NA    2    2
[3,]    1   NA    2    1
> dat[ complete.cases(dat), ]
[1] 1 1 1 1
> dat[ ! complete.cases(dat), ]
     [,1] [,2] [,3] [,4]
[1,]   NA   NA    2    2
[2,]    1   NA    2    1
于 2013-02-25T06:23:02.270 に答える