[R] The permutation of one vector into another

Niels Richard Hansen Niels.R.Hansen+lists at math.ku.dk
Thu Sep 16 09:13:33 CEST 2010


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

-- 
Niels Richard Hansen                     Web:   www.math.ku.dk/~richard	
Associate Professor                      Email: Niels.R.Hansen at math.ku.dk
Department of Mathematical Sciences             nielsrichardhansen at gmail.com
University of Copenhagen                 Skype: nielsrichardhansen.dk	
Universitetsparken 5                     Phone: +45 353 20783 (office)	
2100 Copenhagen Ø                               +45 2859 0765 (mobile)
Denmark



More information about the R-help mailing list