order_df: Create an ordered factor in a data.frame and sort the...

View source: R/order_df.R

order_dfR Documentation

Create an ordered factor in a data.frame and sort the data.frame by that column. This function stills work but will be removed (defunct) in the next version of the package. You can use forcats::fct_reorder for the same functionality.

Description

Can use the summary of another column (target_col), or specify the order manually (factor_order).

Usage

order_df(
  df,
  target_col,
  value_col = NULL,
  fun = NULL,
  ...,
  desc = FALSE,
  factor_order = NULL
)

Arguments

df

The data frame to sort.

target_col

The column (character or factor) to convert to ordered factor.

value_col

The column to use to sort target_col.

fun

Function to use to summarize value_col.

...

Other options passed on to fun (eg. na.rm = TRUE).

desc

Should target_col be sorted according to value_col in a descending order (default FALSE).

factor_order

A character vector of the unique values in target_col in the desired order.

Value

data frame

Examples

set.seed(42)
df <- data.frame(Col1 = rep(letters[1:3], 3), 
                 Col2 = rnorm(9), 
                 Col3 = 1:9, 
                 stringsAsFactors = FALSE)

suppressWarnings(order_df(df, target_col = "Col1", value_col = "Col2", fun = mean))
suppressWarnings(order_df(df, target_col = "Col1", factor_order = c("b", "a", "c")))

bcgov/envreportutils documentation built on Feb. 1, 2024, 8:48 p.m.