In the following code snippet, data is ordered by col1
followed by col2
, I want to make the order statement generic so that if data has additional columns e.g. col3
the same order statement will work to sort the data by the additional columns in the order they appear (so, order by col1
, then col2
, then col3
). Essentially need to make order statement dynamic
df <- cbind(c("c","a","b"))
df <- cbind(df, c(2,3,1))
df <- as.data.frame(df)
names(df)[1] <- "col1"
names(df)[2] <- "col2"
df[order(df$col1, df$col2),]