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