[R] Sorting data in R according to the header of another table

R. Michael Weylandt <michael.weylandt@gmail.com> michael.weylandt at gmail.com
Mon Oct 3 17:06:54 CEST 2011

Try this:

X = 1:5; names(X) = letters[sample(5)]
Y = matrix(1:25, 5); colnames(Y) = letters[1:5]

Y[ , names(X)]

Hope this helps,

Michael Weylandt

On Oct 3, 2011, at 6:20 AM, "Samir Benzerfa" <benzerfa at gmx.ch> wrote:

> Hi everyone,
>
>
>
> My (simplified) problem is the following one: I have two tables. The first
> table contains 5 columns with 5 values and the second table contains a
> single value for each vector name of the first table (see tables below):
>
>
>
> Table 1:
>
>
>
> A             B             C             D             E
>
> 1             2             3             4             5
>
> 2             3             4             5             6
>
> 3             4             5             6             7
>
> 4             5             6             7             8
>
> 5             6             7             8             9
>
>
>
>
>
> Table 2:
>
>
>
> A             B             C             D             E
>
> 3             5             2             1             4
>
>
>
> My goal is to first sort the values of Table 2 and then use the new header
> of it to sort the columns of Table 1 according to that. I already sorted
> Table 2 by using the sort(Table2) function getting the following result:
>
>
>
> D             C             A             E             B
>
> 1             2             3             4             5
>
>
>
> How can I now sort the columns in Table 1 according to the header of the new
> sorted Table 2. That is, have column D in the first position of Table 1,
> column C in the second, and forth.
>
>
>
> Many thanks for your assistance!
>
> S.B.
>
>
>
>
>    [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help