Arrange a daat frame by one of its columns, either in ascending or descending order.
Arguments
- df
The data frame to sort.
- colname
The column to sort with.
- desc
Whether to sort in descending order.
Examples
data <- tibble::tibble(
x = 10:1,
y = c(letters[6:10], letters[5:1])
)
sort_df(data, "x")
#> # A tibble: 10 × 2
#> x y
#> <int> <chr>
#> 1 1 a
#> 2 2 b
#> 3 3 c
#> 4 4 d
#> 5 5 e
#> 6 6 j
#> 7 7 i
#> 8 8 h
#> 9 9 g
#> 10 10 f
sort_df(data, "y", desc = TRUE)
#> # A tibble: 10 × 2
#> x y
#> <int> <chr>
#> 1 6 j
#> 2 7 i
#> 3 8 h
#> 4 9 g
#> 5 10 f
#> 6 5 e
#> 7 4 d
#> 8 3 c
#> 9 2 b
#> 10 1 a