根据数据框中的另一行选择一行

时间:2016-05-13 16:54:24

标签: r

我的数据看起来像这样

df <- structure(list(V1 = 1:15, V2 = structure(c(5L, 9L, 7L, 8L, 10L, 
2L, 13L, 3L, 11L, 12L, 15L, 1L, 4L, 14L, 6L), .Label = c("A0A087WNY6", 
"B2RTL5", "B8JJX9", "D3Z2H7", "E9PZ97", "G3UWX1", "Q2VWQ4", "Q3TMB5", 
"Q3TWK2", "Q6ZPS9", "Q7TMW3", "Q8BP71", "Q8R4K2", "Q925B0", "Q9WU01"
), class = "factor"), V3 = c(5L, 7L, 10L, 11L, 13L, 15L, NA, 
NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("V1", "V2", "V3"
), class = "data.frame", row.names = c(NA, -15L))

我想根据第三列值

选择前两列中的行

预期的输出是这个

5   Q6ZPS9
7   Q8R4K2
10  Q8BP71
11  Q9WU01
13  D3Z2H7
15  G3UWX1

1 个答案:

答案 0 :(得分:6)

我觉得V3不应该在这个数据框中,而是一个不同的向量。但这是一种方式

df[df$V1 %in% df$V3,1:2]