如何找到两组数据帧之间的匹配索引

时间:2016-08-16 09:29:57

标签: r

两个数据框,其中一个是我的参考

df1<- structure(list(V1 = structure(c(2L, 14L, 8L, 12L, 1L, 3L, 4L, 
5L, 6L, 9L, 10L, 16L, 7L, 15L, 11L, 13L), .Label = c("A", "AbC", 
"B", "C", "D", "F", "FFFS", "G6_7", "GI666", "GTJJJ", "HINDO", 
"MirTn", "Mumbai", "NdFi1", "TRS100", "TTTNKK"), class = "factor"), 
    V2 = c(10L, 22L, 33L, 35L, 89L, 6L, 973L, 686L, 82L, 22L, 
    1L, 82L, 1L, 9304L, 43L, 736L)), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, 
-16L))


df2<- structure(list(V1 = structure(c(1L, 4L, 5L, 3L, 2L, 6L), .Label = c("AbC", 
"Bangalore", "Dehli", "F", "GI666", "Mumbai"), class = "factor")), .Names = "V1", class = "data.frame", row.names = c(NA, 
-6L))

我想找到匹配的索引,以及df1 $ V1和df2 $ V1

之间不匹配的破折号

我尝试这样做没有成功,这是因为R正在重复列

上的索引
df1$myindex <- as.character(which(df1$V1 %in% df2$V1))

我正在寻找的内容如下所示

#         V1 myindex 
#1       AbC    1
#2         F    9
#3     GI666    10
#4     Dehli    -
#5 Bangalore    -
#6    Mumbai    16

1 个答案:

答案 0 :(得分:1)

您可以使用match

match(df2$V1, df1$V1)
#[1]  1  9 10 NA NA 16

如果您不想NA并希望-,则可以使用ifelse

i1 <- match(df2$V1, df1$V1)
df2$myindex <- ifelse(is.na(i1), "-", i1)
df2
#         V1 myindex
#1       AbC       1
#2         F       9
#3     GI666      10
#4     Dehli       -
#5 Bangalore       -
#6    Mumbai      16