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
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.> > HaroldDavid Winsemius, MD West Hartford, CT
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/