[R] re-ordering a vector by name

Huntsinger, Reid reid_huntsinger at merck.com
Fri May 7 23:30:59 CEST 2004


I suspect three causes for slowness:

1) possibly names are lot of overhead (string compares, lookup, etc).
2) maybe it's just memory, in which case you could loop over chunks of the
names(x1) vector
3) you're basically asking for the permutation taking names(x2) into
names(x1) and then applying it to x2. The first step is a sort but perhaps
the indexing code doesn't optimize that.

Reid Huntsinger
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Liaw, Andy
Sent: Friday, May 07, 2004 5:16 PM
To: r-help at stat.math.ethz.ch
Subject: [R] re-ordering a vector by name


Dear R-help,

Let's say `x1' and `x2' are very long vectors (length=5e5, say) with same
set of names but in different order.  If I want to sort `x2' in the order of
`x1', I would do 

  x2[names(x1)]

but the amount of time that takes is quite prohibitive!  Does anyone have
any suggestion on a more efficient way to do this?

If the two vectors are exactly the same length (as I said above), sorting
both by names would probably be the fastest.  However, if the two vectors
differ in length (and the names for the shorter one are a subset of names of
the longer one) then that doesn't work...

Best,
Andy

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html

----------------------------------------------------------------------------
--
Notice:  This e-mail message, together with any attachments,...{{dropped}}




More information about the R-help mailing list