I wrote this tiny little wrapper around order
, but I fear my implementation is lame. I huddle in the corner, waiting for the gods of R commands or algorithmic efficiency to smite my ergonomic keyboard :-(
set.seed(1001)
height <- rnorm(6, mean = 1, sd = 0.2)
weight <- rnorm(6, mean = 100, sd = 15)
id <- 1:6
dd <- data.frame(id, height, weight)
# Here's the function I came up with
ReorderDataByColumn <- function(x, column) {
ordered.indices <- order(x[ ,paste(column)])
return(x[ordered.indices, ])
}
#And here are its results
> ReorderDataByColumn(dd, column = "height")
id height weight
4 4 0.4986928 76.09430
5 5 0.8885377 104.53967
3 3 0.9629449 86.38809
2 2 0.9644905 90.65584
6 6 0.9712881 124.51589
1 1 1.4377296 116.37253
> ReorderDataByColumn(dd, column = "weight")
id height weight
4 4 0.4986928 76.09430
3 3 0.9629449 86.38809
2 2 0.9644905 90.65584
5 5 0.8885377 104.53967
1 1 1.4377296 116.37253
6 6 0.9712881 124.51589