If I have a large DF (hundreds and hundreds) columns with different col_names randomly distributed alphabetically:
df.x <- data.frame(2:11, 1:10, rnorm(1
If a specific column (or columns) has to be the first one (or last), but the rest is ordered, you can:
mtcars %>% tibble %>%
select("hp", sort(colnames(.)))
Why not just:
sort(colnames(df.x))
[1] "delta" "ID" "string"
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.
Try this
df %>% select(noquote(order(colnames(df))))
or just
df[,order(colnames(df))]