简体   繁体   中英

select subjects from a list in r

I have a dataset as "testData"

testData
ID       v1    v2
8836434  ..    ..
8868426  .     .
8868457 
8868519 
8868550 
8868581 
8868643 
8878687
8879555 
8878749 
8878780 
8878811 

I want to select those subjects that appear in the following list:

selectList
ID       
8868519  
8868550  
8868581 
8868643 
8878687

I tried

testData[ testData$ID == selectList$ID, ]

and

testData[which(testData$ID == selectList$ID), ] 

But they don't work properly. Any suggestions?

Simply following @akrun and @Richard Scriven's instructions,

testData <- data.frame(ID = c("8836434", "8868426", "8868457", "8868519", "8868550", 
                              "8868581", "8868643", "8878687", "8878749", "8878780", 
                               "8878811", "8879555"), 
                       V1 = sample(c(1, 2, 3, NA, 5, 6),  12, rep=TRUE), 
                       V2 = sample(c(11, 12, 13, 14, 15, 16),  12, rep=TRUE))

selectList <- data.frame(ID = c(8868519, 8868550, 8868581, 8868643, 8878687))

testData[testData$ID %in% selectList$ID, ]

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