Question

If I have a list of data frames

list.dfs <- list(df1 = data.frame(var1 = c(1:3), var2 = c(1:3), var3 = c(1:3)), 
      df2 = data.frame(var1= c(1:3), var2 = c(1:3), var3 = c(1:3)), 
      df3 = data.frame(var1= c(1:3), var2 = c(1:3), var3 = c(3:1)))

How do I use lapply and order to sort every data frame in the list by var3 (lowest to highest)

Was it helpful?

Solution

lapply(list.dfs, function(x) x[order(x$var3), ])

will do the trick.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top