[R] Getting the difference between two data frames

Rolf Turner r.turner at auckland.ac.nz
Thu Feb 19 23:58:29 CET 2009


On 20/02/2009, at 11:23 AM, Ferry wrote:

> Dear R users,
>
> I have the following data:
> x <- data.frame( myX = c(1,2,3,4,5,6,7,8,9) )
> y <- data.frame( myX = c(1,2,3,4,5,6,7) )
>
> How can I get the difference between data frame x and y? In this case,
> I want to get values 8 and 9
>
> I know in SQL we can use minus operator, but I have no idea how to  
> do so in R.
>
> I tried all.equal, diff, and identical, but they don't give me the
> actual data difference.

First of all, you ***don't*** want the ``difference between two data  
frames''.
What you (apparently) want is the ***set difference*** between two  
***vectors***
each of which is (irrelevantly) stored inside a different data frame.

You can get what you want using the function setdiff().  E.g.:

 > setdiff(x$myX,y$myX)
[1] 8 9


Data frames as such do not come into it.  You are just confusing the  
issue
by referring to them.  I.e. you could have much more simply said:

``
x <- c(1,2,3,4,5,6,7,8,9)
y <- c(1,2,3,4,5,6,7)

How do I get the difference c(8,9)?''

(Answer: setdiff(x,y).)

	cheers,

		Rolf



######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}




More information about the R-help mailing list