dply: order columns alphabetically in R
Try this
df %>% select(noquote(order(colnames(df))))
or just
df[,order(colnames(df))]
Update Dec 2021
New versions of dplyr
(>= 1.0.7) work without the noquote
:
df %>% select(order(colnames(df)))
An alternative way to do this in dplyr is:
iris %>%
select(sort(current_vars()))
current_vars()
returns column names such that they're sortable, and select()
will take the vector of column names.