Names of rows in an array in R

1

I have an array that looks like this:

LH_CV    0.044919900
LH_IQ    0.457549906
LH_Kur   0.007265657
LH_Max   0.572179944
LH_Mean  0.592315043
LH_Min  -0.053051707
LH_Mode  0.352844542
LH_P01   0.107923404
LH_P05   0.292272397
LH_P10   0.356974061
LH_P20   0.450763661
LH_P25   0.482754310
LH_P30   0.511708889
LH_P40   0.544075112
LH_P50   0.571894184
LH_P60   0.586855536
LH_P70   0.592084683
LH_P75   0.592076738
LH_P80   0.590268021
LH_P90   0.581416025
LH_P95   0.577601830
LH_P99   0.572313030

And the thing is that I would like to order the values from highest to lowest but that I would continue to put the names of the rows. When doing so with the function sort() , it removes them.

Thanks in advance.

    
asked by alba 03.07.2018 в 12:11
source

1 answer

1

You can use order to maintain the format of the matrix.

Reproducible example:

m <- as.matrix(anscombe)
rownames(m) <- letters[1:11]
m
  x1 x2 x3 x4    y1   y2    y3    y4
a 10 10 10  8  8.04 9.14  7.46  6.58
b  8  8  8  8  6.95 8.14  6.77  5.76
c 13 13 13  8  7.58 8.74 12.74  7.71
d  9  9  9  8  8.81 8.77  7.11  8.84
e 11 11 11  8  8.33 9.26  7.81  8.47
f 14 14 14  8  9.96 8.10  8.84  7.04
g  6  6  6  8  7.24 6.13  6.08  5.25
h  4  4  4 19  4.26 3.10  5.39 12.50
i 12 12 12  8 10.84 9.13  8.15  5.56
j  7  7  7  8  4.82 7.26  6.42  7.91
k  5  5  5  8  5.68 4.74  5.73  6.89

Solution: In this example, I sort the matrix (called m ) with the data in the first row. For this I use order , which returns the positions of the order of the input vector, inside the brackets to obtain the matrix with the same format, but with the ordered positions.

m[order(m[,1]),]
  x1 x2 x3 x4    y1   y2    y3    y4
h  4  4  4 19  4.26 3.10  5.39 12.50
k  5  5  5  8  5.68 4.74  5.73  6.89
g  6  6  6  8  7.24 6.13  6.08  5.25
j  7  7  7  8  4.82 7.26  6.42  7.91
b  8  8  8  8  6.95 8.14  6.77  5.76
d  9  9  9  8  8.81 8.77  7.11  8.84
a 10 10 10  8  8.04 9.14  7.46  6.58
e 11 11 11  8  8.33 9.26  7.81  8.47
i 12 12 12  8 10.84 9.13  8.15  5.56
c 13 13 13  8  7.58 8.74 12.74  7.71
f 14 14 14  8  9.96 8.10  8.84  7.04
    
answered by 03.07.2018 в 15:10