Hello all, I think I am missing something about the sorting parameter in the "match" command/ Here is an example: a1 <- data.frame(name = c("D", "B", "C", "A", "A", "C")) a2 <- data.frame(name = c("A", "B", "C", "D"), num = 1:4) a1 a2 merge(a1, a2, sort = F, by.x = T) The result is: name num 1 D 4 2 B 2 3 C 3 4 C 3 5 A 1 6 A 1 While I wish my rows to be in the same order as in a1, they are having some other order. What am I missing here? Thanks. ----------------Contact Details:------------------------------------------------------- Contact me: Tal.Galili@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- [[alternative HTML version deleted]]
Missing: a closer reading of the help page -- Value A data frame. The rows are by default lexicographically sorted on the common columns, but for sort = FALSE are in an unspecified order. So sort = FALSE says unspecified. If you want the original order, then add a column to the dataframe with the order and then sort the result. On Mon, Nov 8, 2010 at 4:09 PM, Tal Galili <tal.galili at gmail.com> wrote:> Hello all, > > I think I am missing something about the sorting parameter in the "match" > command/ > Here is an example: > > > a1 <- data.frame(name = c("D", "B", "C", "A", "A", "C")) > a2 <- data.frame(name = c("A", "B", "C", "D"), num = 1:4) > a1 > a2 > merge(a1, a2, sort = F, by.x = T) > > > > The result is: > > ?name num > 1 ? ?D ? 4 > 2 ? ?B ? 2 > 3 ? ?C ? 3 > 4 ? ?C ? 3 > 5 ? ?A ? 1 > 6 ? ?A ? 1 > > > While I wish my rows to be in the same order as in a1, they are having some > other order. > > What am I missing here? > > > Thanks. > > > ----------------Contact > Details:------------------------------------------------------- > Contact me: Tal.Galili at gmail.com | ?972-52-7275845 > Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | > www.r-statistics.com (English) > ---------------------------------------------------------------------------------------------- > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Tena koe Tal sort: logical. Should the results be sorted on the 'by' columns? Thus it is clear what sort=TRUE does, sort=FALSE does not do this. That doesn't mean it leaves the result in the same order as x (your a1), although many people (including me at first) assume it does. It is easy enough to write a wrapper function to retain the order of x in cases like yours. For example: mergeRO <- function (x, y, ...) { myMerge <- merge(cbind(x, myOrder = 1:nrow(x)), y, ...) attr(myMerge, "creation time") <- Sys.time() myMerge[order(myMerge$myOrder), colnames(myMerge) != "myOrder"] } but I'm not sure this will work in all circumstances in which people might use merge(). HTH ... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Tal Galili > Sent: Tuesday, 9 November 2010 10:10 a.m. > To: r-help at r-project.org > Subject: [R] Help with getting ?match to not sort > > Hello all, > > I think I am missing something about the sorting parameter in the > "match" > command/ > Here is an example: > > > a1 <- data.frame(name = c("D", "B", "C", "A", "A", "C")) > a2 <- data.frame(name = c("A", "B", "C", "D"), num = 1:4) > a1 > a2 > merge(a1, a2, sort = F, by.x = T) > > > > The result is: > > name num > 1 D 4 > 2 B 2 > 3 C 3 > 4 C 3 > 5 A 1 > 6 A 1 > > > While I wish my rows to be in the same order as in a1, they are having > some > other order. > > What am I missing here? > > > Thanks. > > > ----------------Contact > Details:------------------------------------------------------- > Contact me: Tal.Galili at gmail.com | 972-52-7275845 > Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) > | > www.r-statistics.com (English) > ----------------------------------------------------------------------- > ----------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.The contents of this e-mail are confidential and may be subject to legal privilege. If you are not the intended recipient you must not use, disseminate, distribute or reproduce all or any part of this e-mail or attachments. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail. Any opinion or views expressed in this e-mail are those of the individual sender and may not represent those of The New Zealand Institute for Plant and Food Research Limited.
Ted - If you want to retain the exact order of the rows of one of the merged matrices, I think you have to merge them "by hand":> cbind(a1,num=a2[match(a1$name,a2$name),'num'])name num 1 D 4 2 B 2 3 C 3 4 A 1 5 A 1 6 C 3 - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Mon, 8 Nov 2010, Tal Galili wrote:> Hello all, > > I think I am missing something about the sorting parameter in the "match" > command/ > Here is an example: > > > a1 <- data.frame(name = c("D", "B", "C", "A", "A", "C")) > a2 <- data.frame(name = c("A", "B", "C", "D"), num = 1:4) > a1 > a2 > merge(a1, a2, sort = F, by.x = T) > > > > The result is: > > name num > 1 D 4 > 2 B 2 > 3 C 3 > 4 C 3 > 5 A 1 > 6 A 1 > > > While I wish my rows to be in the same order as in a1, they are having some > other order. > > What am I missing here? > > > Thanks. > > > ----------------Contact > Details:------------------------------------------------------- > Contact me: Tal.Galili at gmail.com | 972-52-7275845 > Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | > www.r-statistics.com (English) > ---------------------------------------------------------------------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >