sort_cols: Sort columns in a dataframe

View source: R/dataframe_tools.R

sort_colsR Documentation

Sort columns in a dataframe

Description

Sorts the columns of a dataframe, and then allows you to pull columns to the start of the dataframe by name.

Usage

sort_cols(df, ..., decreasing = FALSE)

Arguments

df

(Dataframe) A dataframe.

...

(Column names) If you want to manually position columns after they are sorted, provide unquoted column names here. The columns in ... will be placed first in the dataframe, and then all other unlisted columns will be placed after.

decreasing

(Logical) If FALSE, sort columns from A-Z and 0-9. If TRUE, sort in reverse.

Value

A copy of df with reordered columns.

Authors

Examples

colnames(iris)

#> [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"

sorted <- sort_cols(iris)
colnames(sorted)

#> [1] "Petal.Length" "Petal.Width"  "Sepal.Length" "Sepal.Width"  "Species"

reverse <- sort_cols(iris, decreasing = TRUE)
colnames(reverse)

#> [1] "Species"  "Sepal.Width"  "Sepal.Length" "Petal.Width"  "Petal.Length"

manual <- sort_cols(iris, Species)
colnames(manual)

#> [1] "Species" " Petal.Length" "Petal.Width"  "Sepal.Length" "Sepal.Width"


DesiQuintans/desiderata documentation built on April 9, 2023, 5:43 a.m.