Find complement of a data frame (anti - join)

前端 未结 7 1062
情深已故
情深已故 2020-11-21 11:20

I have two data frames(df and df1). df1 is subset of df. I want to get a data frame which is complement of df1 in df, i.e. return rows of the first data set which are not ma

7条回答
  •  我寻月下人不归
    2020-11-21 12:17

    Another option by creating a function negate_match_df by manipulating the code of match_df of plyr package.

    library(plyr)
    negate_match_df <- function (x, y, on = NULL) 
    {
    if (is.null(on)) {
        on <- intersect(names(x), names(y))
        message("Matching on: ", paste(on, collapse = ", "))
    }
    keys <- join.keys(x, y, on)
    x[!keys$x %in% keys$y, , drop = FALSE]
    }
    

    Data

    df <- read.table(text ="heads
    row1
    row2
    row3
    row4
    row5",header=TRUE)
    
    df1 <- read.table(text ="heads
    row3
    row5",header=TRUE)
    

    Output

    negate_match_df(df,df1)
    

提交回复
热议问题