R: remove rows from one data frame that are in another

前端 未结 3 933
情书的邮戳
情书的邮戳 2021-02-08 21:32

I have two data frames df1 and df2. They have the same (two) columns. I want to remove the rows from df1 that are in df2.

3条回答
  •  情书的邮戳
    2021-02-08 22:20

    You can do that with several packages. But here's how to do it with base R.

    df1 <-matrix(1:6,ncol=2,byrow=TRUE)
    df2 <-matrix(1:10,ncol=2,byrow=TRUE)
    all <-rbind(df1,df2) #rbind the columns
    #use !duplicated fromLast = FALSE and fromLast = TRUE to get unique rows.
    all[!duplicated(all,fromLast = FALSE)&!duplicated(all,fromLast = TRUE),] 
    
         [,1] [,2]
    [1,]    7    8
    [2,]    9   10
    

提交回复
热议问题