[R] Help with getting ?match to not sort

Phil Spector spector at stat.berkeley.edu
Mon Nov 8 23:13:08 CET 2010


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.
>



More information about the R-help mailing list