简体   繁体   中英

subset.data.frame in R

I have a data frame of raw data:

raw <- data.frame(subj = c(1,1,1,2,2,2,3,3,3,4,4,4),
                   blah = c(0,0,0,1,1,1,1,0,1,0,0,0))

From it, I want to remove the bad subj.

badsubj <- c(1,4)  
trim <- subset.data.frame(raw, subj != badsubj)  

But for some reason, all the badsubj values are not removed:

   subj blah
2     1    0
4     2    1
5     2    1
6     2    1
7     3    1
8     3    0
9     3    1
11    4    0

What am I doing wrong? Obersvations 2 and 11 should be excluded because they are members of badsubj.

raw[!raw$subj %in% badsubj, ] 

错误使用!=

The problem is that subj and badsubj do not have the same length. Therefore badsubj will be recycled until both vectors have the same length. Then your code compares elementwise the values in the output below.

     subj badsubj
 1     1    1
 2     1    4
 3     1    1
 4     2    4
 5     2    1
 6     2    4
 7     3    1
 8     3    4
 9     3    1
 10    4    4
 11    4    1
 12    4    4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM