[R] The permutation of one vector into another

Niels Richard Hansen Niels.R.Hansen+lists at math.ku.dk
Thu Sep 16 10:56:20 CEST 2010


> match(b, a) ?
>
> Michael

Right! Thanks, Michael.

>
>
> On 16 September 2010 17:13, Niels Richard Hansen
> <Niels.R.Hansen+lists at 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
>> >



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