how to find index of match between two set of data frame

后端 未结 1 1795
野性不改
野性不改 2020-12-04 02:47

Two data frame with one being my reference

df1<- structure(list(V1 = structure(c(2L, 14L, 8L, 12L, 1L, 3L, 4L, 
5L, 6L, 9L, 10L, 16L, 7L, 15L, 11L, 13L),         


        
相关标签:
1条回答
  • 2020-12-04 03:25

    You can use match

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

    If you do not want NA and want it as -, you can use 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
    
    0 讨论(0)
提交回复
热议问题