如何在两组字符串之间找到索引?

时间:2017-08-03 12:29:25

标签: r

我有两个数据看起来像这样

df1<- structure(list(V1 = structure(c(6L, 1L, 8L, 20L, 12L, 5L, 21L, 
28L, 14L, 3L, 15L, 18L, 17L, 25L, 26L, 16L, 10L, 7L, 2L, 13L, 
23L, 27L, 24L, 4L, 19L, 11L, 9L, 22L), .Label = c("O43175", "P02538", 
"P04264", "P04350", "P07437", "P08237", "P08238", "P11142", "P11498", 
"P13645", "P25705-1", "P31327-1", "P31689", "P35527", "P35555", 
"P35908", "P68104", "P68366", "P68371", "P78527", "Q01813", "Q13509", 
"Q13885", "Q15233", "Q15418-2", "Q70CQ2", "Q71U36", "Q9BQE3"), class = "factor")), .Names = "V1", class = "data.frame", row.names = c(NA, 
-28L))

我想找到df1中存在于df1

中的字符串索引
df2<- structure(list(V1 = structure(c(5L, 4L, 3L, 2L, 1L), .Label = c("P02538", 
"P08238", "P13645", "P35908", "Q70CQ2"), class = "factor")), .Names = "V1", class = "data.frame", row.names = c(NA, 
-5L))

我做以下

match(df1$V1,df2$V1)

在这种情况下,它可以工作,但是当数据很大并且我一个接一个地查看它们时,我发现它与其他东西相匹配, 有没有其他方法来检查这个?或者完全匹配?

我不知道什么是不对的。

如何在这样的数据中找到值的索引

df<- structure(c(NA, NA, NA, NA, NA, NA, NA, 12L, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 27L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA), .Dim = c(100L, 1L))

我试过which(is.na(df))它没有给我索引。我只需知道它们是哪些行并将其保存在data.frame

1 个答案:

答案 0 :(得分:0)

尝试

df3<-cbind(df1,pos=c(1:nrow(df1)))  
merge(df3,df2)  

输出

      V1 pos
1 P02538  19
2 P08238  18
3 P13645  17
4 P35908  16
5 Q70CQ2  15