Usage
order_col(df, col, levels = NULL)
Arguments
- df
Data frame to transform
- col
column to transform
- levels
levels for the column, in ascending order.
If NULL
(default) use the current ordering in the column.
Value
A copy of the data frame with the col
column transformed
into an ordered factor with levels given by levels
.
Examples
df <- tibble::tibble(
x = c("b", "c", "a", "c", "b", "a", "a"),
y = rnorm(7),
z = 5
)
new_df <- df |>
order_col("x", c("c", "b", "a")) |>
dplyr::select("x", "z") |>
dplyr::arrange(x)
new_df
#> # A tibble: 7 × 2
#> x z
#> <ord> <dbl>
#> 1 c 5
#> 2 c 5
#> 3 b 5
#> 4 b 5
#> 5 a 5
#> 6 a 5
#> 7 a 5