Monday, 7 March 2016

r - how to find index between two set of strings?



I have two datas look like this



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))


I want to find the index of the strings from df2 that exist in the 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))


I do the following



match(df1$V1,df2$V1)



In this case, it works but when the data is huge and I look at them one by one, I see it matched to something else,
is there any other way to check this? or make this exact match ?



I don't know what is wrong that does not work.



How can I find the index for the values in such a data



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))


I tried which(is.na(df)) it does not give me the index . I only need to know which rows they are and save it in a data.frame


Answer



try




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


Output



      V1 pos
1 P02538 19
2 P08238 18

3 P13645 17
4 P35908 16
5 Q70CQ2 15

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...