[R] Compare two data frames

Bert Gunter gunter.berton at gene.com
Thu Apr 22 22:29:51 CEST 2010


Why does:

nm <- intersect(names(d1),names(d2))
identical(d1[,nm], d2[,nm])

not do it?

-- Bert

Bert Gunter
Nonclinical Biostatistics
467-7374

http://devo.gene.com/groups/devo/depts/ncb/home.shtml

 

 

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Paul Murrell
Sent: Thursday, April 22, 2010 12:54 PM
To: Doran, Harold
Cc: r help
Subject: Re: [R] Compare two data frames

Hi

Sounds like the 'compare' package might do some of what you want.

Paul

On 23/04/2010 1:41 a.m., 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.
>
> Harold
>
>> sessionInfo()
> R version 2.10.1 (2009-12-14) i386-pc-mingw32
>
> locale: [1] LC_COLLATE=English_United States.1252
> LC_CTYPE=English_United States.1252    LC_MONETARY=English_United
> States.1252 [4] LC_NUMERIC=C
> LC_TIME=English_United States.1252
>
> attached base packages: [1] stats     graphics  grDevices utils
> datasets  methods   base
>
> other attached packages: [1] MiscPsycho_1.6 statmod_1.4.6
>
> loaded via a namespace (and not attached): [1] tools_2.10.1
>
> ______________________________________________ R-help at r-project.org
> mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do
> read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list