0

I have this matrix with a range of values. I am planning on plotting columns 2 to 6 against the values in column 1. I have seen that it is possible to sort columns of data. How would I sort the data in each row in size order but ignore the data in column 1? E.g The first row would look like

          [,1]       [,2]         [,3]       [,4]       [,5]       [,6]
     [1,]   1      0.05091557  0.05777423 0.07875043  0.08011981  0.09525210 

Is there anything I should be weary of when sorting matrices this way?

      [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
 [1,]    1 0.05091557 0.07875043 0.08011981 0.09525210 0.05777423
 [2,]    2 0.01124827 0.11040450 0.15189661 0.17496065 0.05869832
 [3,]    3 0.10205376 0.04264597 0.03424936 0.29083599 0.08650822
 [4,]    4 0.12625884 0.02630636 0.08814499 0.06744327 0.04713059
 [5,]    5 0.02588615 0.31805945 0.09965736 0.11084963 0.08008907
 [6,]    6 0.03488529 0.03275217 0.10867476 0.02974519 0.16357899
 [7,]    7 0.07651835 0.04900895 0.17309055 0.13379696 0.11079284
 [8,]    8 0.04576330 0.04437211 0.14715493 0.17329859 0.19364271
 [9,]    9 0.01288462 0.27600904 0.08185839 0.11899927 0.03254210
[10,]   10 0.05267915 0.10229029 0.03192866 0.01621034 0.16809146
Nhyi
  • 373
  • 1
  • 12

1 Answers1

0

Try this. As you are working with a matrix, you can use apply(). In this function you need to set a range in the matrix. In your case it would be from column two to the last column you have. As the output is transposed you can use t() to give the original form as in the initial matrix and then combine with the first column using cbind(). Here the code:

#Code
mat2 <- cbind(mat[,1],t(apply(mat[,2:6],1,function(x) sort(x))))

Output:

mat2
      [,1]       [,2]       [,3]       [,4]       [,5]      [,6]
 [1,]    1 0.05091557 0.05777423 0.07875043 0.08011981 0.0952521
 [2,]    2 0.01124827 0.05869832 0.11040450 0.15189661 0.1749606
 [3,]    3 0.03424936 0.04264597 0.08650822 0.10205376 0.2908360
 [4,]    4 0.02630636 0.04713059 0.06744327 0.08814499 0.1262588
 [5,]    5 0.02588615 0.08008907 0.09965736 0.11084963 0.3180595
 [6,]    6 0.02974519 0.03275217 0.03488529 0.10867476 0.1635790
 [7,]    7 0.04900895 0.07651835 0.11079284 0.13379696 0.1730906
 [8,]    8 0.04437211 0.04576330 0.14715493 0.17329859 0.1936427
 [9,]    9 0.01288462 0.03254210 0.08185839 0.11899927 0.2760090
[10,]   10 0.01621034 0.03192866 0.05267915 0.10229029 0.1680915

Some data used:

#Data
mat <- structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0.05091557, 0.01124827, 
0.10205376, 0.12625884, 0.02588615, 0.03488529, 0.07651835, 0.0457633, 
0.01288462, 0.05267915, 0.07875043, 0.1104045, 0.04264597, 0.02630636, 
0.31805945, 0.03275217, 0.04900895, 0.04437211, 0.27600904, 0.10229029, 
0.08011981, 0.15189661, 0.03424936, 0.08814499, 0.09965736, 0.10867476, 
0.17309055, 0.14715493, 0.08185839, 0.03192866, 0.0952521, 0.17496065, 
0.29083599, 0.06744327, 0.11084963, 0.02974519, 0.13379696, 0.17329859, 
0.11899927, 0.01621034, 0.05777423, 0.05869832, 0.08650822, 0.04713059, 
0.08008907, 0.16357899, 0.11079284, 0.19364271, 0.0325421, 0.16809146
), .Dim = c(10L, 6L), .Dimnames = list(NULL, NULL))
Duck
  • 39,058
  • 13
  • 42
  • 84