[R] The permutation of one vector into another

Michael Bedward michael.bedward at gmail.com
Thu Sep 16 10:23:57 CEST 2010


match(b, a) ?

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



More information about the R-help mailing list