On Sep 16, 2010, at 4:23 AM, Michael Bedward wrote:

match(b, a) ?

Or just order(.) :

b[order(a)]
[1] "A" "C" "B"


--
David


Michael


On 16 September 2010 17:13, Niels Richard Hansen
<niels.r.hansen+li...@math.ku.dk> wrote:
Dear R-help-list

I have two character vectors

a <- c("A", "B", "C")
b <- c("A", "C", "B")

Then

sapply(a, function(i) grep(i, b))

computes the permutation of the entries in 'b' needed
to bring 'b' into the same order as 'a'.

I have searched around, but haven't been able to find
any existing function that compute this permutation.
Is there such a function doing this -- perhaps more
efficiently than the above?

For those interested, I need the permutation to bring
the rows of a data frame into the same order as the
levels of a factor.

Thanks, Niels

--


David Winsemius, MD
West Hartford, CT

______________________________________________
R-help@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.

Reply via email to