[R] The permutation of one vector into another

David Winsemius dwinsemius at comcast.net
Thu Sep 16 14:49:18 CEST 2010


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


David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list