I have a matrix which I want ordered by every column. Probably better to show the before and after. This is the matrix before.
thematrix_before <- structure(c(0, 0, 0, 0, 0, 0.44, 0.68, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0.42, 0.39, 0, 0, 0, 0, 0, 0.35, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0.55, 0, 0, 0, 0, 0, 0, 0.54, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.41, 0.73, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0.44, 0.69, 0, 0, 0, 0, 0, 0.45, 0.67, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(15L, 8L), .Dimnames = list(
c("rQ1_10", "rQ1_11", "rQ1_13", "rQ1_14", "rQ1_15", "rQ1_16",
"rQ1_17", "rQ1_2", "rQ1_23", "rQ1_24", "rQ1_29", "rQ1_3",
"rQ1_4", "rQ1_5", "rQ1_1"), c("Features", "Innovation",
"KPI", "Knowledge", "Performance", "Presence", "Trust", "VFM"
)))
> thematrix_before
Features Innovation KPI Knowledge Performance Presence Trust VFM
rQ1_10 0.00 0.00 0.00 0 0 0.00 0.00 0.45
rQ1_11 0.00 0.00 0.00 0 0 0.00 0.00 0.67
rQ1_13 0.00 0.00 0.00 0 1 0.00 0.00 0.00
rQ1_14 0.00 0.42 0.00 0 0 0.00 0.00 0.00
rQ1_15 0.00 0.39 0.00 0 0 0.00 0.00 0.00
rQ1_16 0.44 0.00 0.00 0 0 0.00 0.00 0.00
rQ1_17 0.68 0.00 0.00 0 0 0.00 0.00 0.00
rQ1_2 0.00 0.00 0.55 0 0 0.00 0.00 0.00
rQ1_23 0.00 0.00 0.00 0 0 0.00 0.44 0.00
rQ1_24 0.00 0.00 0.00 0 0 0.00 0.69 0.00
rQ1_29 0.00 0.35 0.00 0 0 0.00 0.00 0.00
rQ1_3 0.00 0.00 0.00 1 0 0.00 0.00 0.00
rQ1_4 0.00 0.00 0.00 0 0 0.41 0.00 0.00
rQ1_5 0.00 0.00 0.00 0 0 0.73 0.00 0.00
rQ1_1 0.00 0.00 0.54 0 0 0.00 0.00 0.00
Below is how the matrix would look. Apologies, if anything is out of place, as I edited this manually to give you an idea of how it would look. It will sort the "Features" column first, then the next column, then the next, through all columns till it ends up like this. The columns are in same position, and the rows have been reorganised.
> thematrix_reordered
Features Innovation KPI Knowledge Performance Presence Trust VFM
rQ1_17 0.68 0.00 0.00 0 0 0.00 0.00 0.00
rQ1_16 0.44 0.00 0.00 0 0 0.00 0.00 0.00
rQ1_14 0.00 0.42 0.00 0 0 0.00 0.00 0.00
rQ1_15 0.00 0.39 0.00 0 0 0.00 0.00 0.00
rQ1_29 0.00 0.35 0.00 0 0 0.00 0.00 0.00
rQ1_2 0.00 0.00 0.55 0 0 0.00 0.00 0.00
rQ1_1 0.00 0.00 0.54 0 0 0.00 0.00 0.00
rQ1_3 0.00 0.00 0.00 1 0 0.00 0.00 0.00
rQ1_13 0.00 0.00 0.00 0 1 0.00 0.00 0.00
rQ1_5 0.00 0.00 0.00 0 0 0.73 0.00 0.00
rQ1_4 0.00 0.00 0.00 0 0 0.41 0.00 0.00
rQ1_24 0.00 0.00 0.00 0 0 0.00 0.69 0.00
rQ1_23 0.00 0.00 0.00 0 0 0.00 0.44 0.00
rQ1_11 0.00 0.00 0.00 0 0 0.00 0.00 0.67
rQ1_10 0.00 0.00 0.00 0 0 0.00 0.00 0.45
I’m going to use this code for multiple different matrixes. Therefore, if possible need a code that is replicable for any matrix, regardless of name of columns. e.g. DF[ order by all names(DF), ].
>Solution :
Using the order function. Watch the commas!
thematrix_before[order(thematrix_before[, 'Features'], thematrix_before[, 'Innovation'], thematrix_before[, 'KPI'], thematrix_before[, 'Knowledge'], thematrix_before[, 'Performance'], thematrix_before[, 'Presence'], thematrix_before[, 'Trust'], thematrix_before[, 'VFM'], decreasing=TRUE), ]
# Features Innovation KPI Knowledge Performance Presence Trust VFM
# rQ1_17 0.68 0.00 0.00 0 0 0.00 0.00 0.00
# rQ1_16 0.44 0.00 0.00 0 0 0.00 0.00 0.00
# rQ1_14 0.00 0.42 0.00 0 0 0.00 0.00 0.00
# rQ1_15 0.00 0.39 0.00 0 0 0.00 0.00 0.00
# rQ1_29 0.00 0.35 0.00 0 0 0.00 0.00 0.00
# rQ1_2 0.00 0.00 0.55 0 0 0.00 0.00 0.00
# rQ1_1 0.00 0.00 0.54 0 0 0.00 0.00 0.00
# rQ1_3 0.00 0.00 0.00 1 0 0.00 0.00 0.00
# rQ1_13 0.00 0.00 0.00 0 1 0.00 0.00 0.00
# rQ1_5 0.00 0.00 0.00 0 0 0.73 0.00 0.00
# rQ1_4 0.00 0.00 0.00 0 0 0.41 0.00 0.00
# rQ1_24 0.00 0.00 0.00 0 0 0.00 0.69 0.00
# rQ1_23 0.00 0.00 0.00 0 0 0.00 0.44 0.00
# rQ1_11 0.00 0.00 0.00 0 0 0.00 0.00 0.67
# rQ1_10 0.00 0.00 0.00 0 0 0.00 0.00 0.45
For any matrix
thematrix_before[do.call('order', c(data.frame(thematrix_before), decreasing=TRUE)), ]
# Features Innovation KPI Knowledge Performance Presence Trust VFM
# rQ1_17 0.68 0.00 0.00 0 0 0.00 0.00 0.00
# rQ1_16 0.44 0.00 0.00 0 0 0.00 0.00 0.00
# rQ1_14 0.00 0.42 0.00 0 0 0.00 0.00 0.00
# rQ1_15 0.00 0.39 0.00 0 0 0.00 0.00 0.00
# rQ1_29 0.00 0.35 0.00 0 0 0.00 0.00 0.00
# rQ1_2 0.00 0.00 0.55 0 0 0.00 0.00 0.00
# rQ1_1 0.00 0.00 0.54 0 0 0.00 0.00 0.00
# rQ1_3 0.00 0.00 0.00 1 0 0.00 0.00 0.00
# rQ1_13 0.00 0.00 0.00 0 1 0.00 0.00 0.00
# rQ1_5 0.00 0.00 0.00 0 0 0.73 0.00 0.00
# rQ1_4 0.00 0.00 0.00 0 0 0.41 0.00 0.00
# rQ1_24 0.00 0.00 0.00 0 0 0.00 0.69 0.00
# rQ1_23 0.00 0.00 0.00 0 0 0.00 0.44 0.00
# rQ1_11 0.00 0.00 0.00 0 0 0.00 0.00 0.67
# rQ1_10 0.00 0.00 0.00 0 0 0.00 0.00 0.45