[R] Compare two data frames

David Winsemius dwinsemius at comcast.net
Thu Apr 22 16:11:16 CEST 2010


On Apr 22, 2010, at 9:41 AM, Doran, Harold wrote:

> I wonder if there is a more efficient way to do this task. Suppose I  
> have two data frames, such as
>
> d1 <- data.frame(x = c(1,2,3), y = c(4,5,6), z = c(7,8,9))
>
> d2 <- d1[, c('y', 'x')]
>
> The first dataframe d1 has more variables than d2 and the variable  
> columns are in a different order. So, what I want to do is compare  
> the two frames on the variables that are common between the two.  
> First I find the common variables between the two dataframes
>
> common_order <- intersect(colnames(d1), colnames(d2))
>
> Then, I have to put the variables in d2 in the same order as d1 as
>
> d2 <- d2[, common_order]
>
> Then, I keep only the variables in common between d1 and d2 as
>
> d1 <- d1[, common_order]
>
> Then, finally I can do the compare on the common variables now in  
> the same order.
>
> all.equal(d1, d2)
>
> None of this is horribly difficult, but it requires a couple of  
> steps that I am wondering might be eliminated.

What you are doing sounds a lot like the operations that merge()  
implements in one function.

-- 
David.
>
> Harold


David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list