I'm trying to find an elegant function to order data.frames held in a list object. I already know that lapply(df, function(x) x[with(x, order(var)), ]) works fine, but that seems way too complicated. I'm trying to use the "[" function, which works fine if I input the row numbers manually. But I'd like to use the row numbers generated by the order function, obviously.
df <- list(
data.frame(name = c("John", "Paul", "George", "Ringo"), height = c(60, 58, 65, 55)),
data.frame(name = c("Frank", "Tony", "Arthur", "Edward"), height = c(55, 65, 60, 50))
)
lapply(df, "[", c("height", "name"))
lapply(df, "[", c(3:1), )
order <- lapply(df, with, order(name))
order
lapply(df, with, order(name), "[")
lapply(df, with, "[", order(name), )
lapply(df, "[", with, order(name), )
Map("[", order , , df)