Find complement of a data frame (anti - join)

前端 未结 7 1145
情深已故
情深已故 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 11:51

    Late answer, but for another option we can try doing a formal SQL anti join, using the sqldf package:

    library(sqldf)
    sql <- "SELECT t1.heads
            FROM df t1 LEFT JOIN df1 t2
                ON t1.heads = t2.heads
            WHERE t2.heads IS NULL"
    df2 <- sqldf(sql)
    

    The sqldf package can be useful for those problems which are easily phrased using SQL logic, but perhaps less easily phrased using base R or another R package.

提交回复
热议问题