Find complement of a data frame (anti - join)

前端 未结 7 1060
情深已故
情深已故 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:01

    Another option, using base R and the setdiff function:

    df2 <- data.frame(heads = setdiff(df$heads, df1$heads))
    

    setdiff functions exactly as you would imagine; take both arguments as sets, and remove all items in the second from the first.

    I find setdiff more readable tahtn %in% and prefer not to require additional libraries when I don't need them, but which answer you use is largely a question of personal taste.

提交回复
热议问题