0

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

1 Answers1

0

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
user2100721
  • 3,557
  • 2
  • 20
  • 29
  • is that possible to improve a bit more? I mean order them (both row and its index accordingly) in nice way. Thx –  Jul 28 '16 at 14:38
  • I also tried like this : base::setdiff(foo, bleh), but it did not match with my expected output. Did you get my output if you used setdiff? It's strange, why I get different output then? –  Jul 28 '16 at 14:43
  • 1
    No, that setdiff function is also from dplyr, and it's a better way to go than anti_join, in my opinion, since it treats each table as a set (ignores dupe rows). – Frank Jul 28 '16 at 14:45
  • Thank you very much. I accept your solution. Cheers –  Jul 28 '16 at 14:47