Please help me on the following code.
set.seed(5)
matrix <- matrix(round(rnorm(100,100,50)), nrow = 4, ncol = 2, byrow = TRUE,
dimnames = list(c("r1", "r2", "r3","r4"),c("c1","c2")))
I need a subset/rows of above matrix where the absolute difference
of row r1
and rest of the rows
in column c1
. If i could sort
the rows by the difference
in increasing order that also will be useful. From there i can find the rows with minimum
difference values.
Input matrix
c1 c2
r1 10 4
r2 6 11
r3 9 17
r4 21 91
Output Matrix
c1 c2
r1 10 4
r2 9 17
r3 6 11
r4 21 91
row r1
remain as reference. row r2 to r3
sorted according to increasing difference from row r1
in column c1
.Any help/clues appreciated.
First, you can calculate the absolute differences between row 1 and all rows (concerning columns 3 and 4) with the following command:
differences <- abs(t(t(matrix[ , 3:4]) - matrix[1, 3:4]))
# c3 c4
# r1 0 0
# r2 39 36
# r3 124 44
# r4 9 11
# r5 75 17
Now you can order these differences by the first column (c3
) in the first place and column 2 (c4
) in the second place. This order is used to order your original matrix
:
matrix[order(differences[ , 1], differences[ , 2]), ]
# c1 c2 c3 c4
# r1 58 169 37 104
# r4 46 92 46 93
# r2 186 70 76 68
# r5 70 -9 112 87
# r3 86 107 161 60
Update based on new example in question:
differences <- abs(t(t(matrix[ , ]) - matrix[1, ]))
# c1 c2
# r1 0 0
# r2 4 7
# r3 1 13
# r4 11 87
matrix[order(differences[ , 1], differences[ , 2]), ]
# c1 c2
# r1 10 4
# r3 9 17
# r2 6 11
# r4 21 91