I'm trying to relocate multiple columns and sort on their value in the final row of my dataset. Is there a way to do this in dplyr, or do I need to swap rows and columns so I can just use arrange?
Here is an example:
df <- data.frame(id = c("q", "w", "e", "t", "d", "r"),
A=c(22, 25, 29, 13, 22, -8),
B=c(12, 10, 6, 6, 8, 11),
C=c(NA, 15, 15, 18, 22, 13),
D=c(90, 4, 3, 6, -6, -6))
And my output would ideally be the same data frame but sorted id, A, D, B, C (in the order of the numbers in the final row).
The character row combined with the negative numbers means that when I try to order the rows using the answer suggested below,
neworder <- order(unlist(df[nrow(df),]))
df[, neworder]
the order produced is D, A, B, C, id. I can't figure out why D and A are swapping places.
CodePudding user response:
Here is a simple method using base R:
df <- data.frame(A=c(22, 25, 29, 13, 22, 30),
B=c(12, 10, 6, 6, 8, 11),
C=c(NA, 15, 15, 18, 22, 13))
#get the last row and establish the order
neworder <- order(unlist(df[nrow(df),]))
df[, neworder]
The new result is order for the lowest to highest based on the values in the last row.
UPDATE To avoid including the first column in the sort, modify the columns passed to the order function. Then add 1 to the result,
#get the last row and establish the order
# selecting only the desired columns
neworder <- order(unlist(df[nrow(df), 2:5]))
#need to add the first column back onto the column
df[, c(1, (neworder 1))]