How to find common rows between two dataframe in R?

前端 未结 5 799
Happy的楠姐
Happy的楠姐 2020-12-30 02:21

I would like to make a new data frame which only includes common rows of two separate data.frame. example:

data.frame 1

1 id300
2 id2345
3 id5456
4 i         


        
5条回答
  •  佛祖请我去吃肉
    2020-12-30 02:59

    We can also do this with fintersect from data.table after converting the data.frame to data.table

    library(data.table)
    fintersect(setDT(df1), setDT(df2))
    #       v1
    #1:  id300
    #2:   id45
    #3: id5456
    

    data

    df1 <- structure(list(v1 = c("id300", "id2345", "id5456", "id33", "id45", 
    "id54")), .Names = "v1", class = "data.frame", row.names = c("1", 
    "2", "3", "4", "5", "6"))
    
    df2 <- structure(list(v1 = c("id832", "id300", "id1000", "id45", "id984", 
    "id5456", "id888")), .Names = "v1", class = "data.frame", row.names = c("1", 
    "2", "3", "4", "5", "6", "7"))
    

提交回复
热议问题