从r中的列表中选择主题

时间:2014-08-01 19:47:38

标签: r

我有一个数据集" testData"

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

我想选择出现在以下列表中的主题:

selectList
ID       
8868519  
8868550  
8868581 
8868643 
8878687

我试过

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

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

但他们不能正常工作。有什么建议吗?

1 个答案:

答案 0 :(得分:2)

只需关注@akrun和@Richard Scriven的说明,

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, ]