rdataframecomplement

How to get relative complement of one data.frame in another?


I have two data.frame objects that need to get its relative complement set from one to another. I checked similar post from this site, certainly someone asked already, but it was about vector, and it is okay. However, I tried solution from existing post in this site, but I did not get my expected output. I tried several approach like setdiff, pmatch vise versa, non of them return my expected output. Can anyone propose possible of way of accomplishing this task efficiently?

example

foo <- data.frame( start=seq(1, by=4, len=6), stop=seq(3, by=4, len=6))
bleh <- data.frame(start=seq(1, by=5, len=5), stop=seq(3, by=5, len=5))

desired output:

According to complement set theory in wiki, my expected output as follows:

  start stop
1     5    7
2     9   11
3    13   15
4    17   19

How can I get relevant complement set of one data.frame to another? what's the correct way to achieving this task? Thanks


Solution

  • Try this

    library(dplyr)
    output <- anti_join(foo,bleh)
    output[order(output$start),]
    

    Another option using setdiff from dplyr package (@Frank Thanks for the correction)

    setdiff(foo,bleh)
    #  start stop
    #1     5    7
    #2     9   11
    #3    13   15
    #4    17   19