Skip to contents

This is used to automate comparison to baselines of various sorts without needing to do all the data wrangling by hand.

Usage

baseline_compare(
  val_df,
  compare_col,
  base_lev,
  values_col,
  group_cols = NULL,
  comp_fun,
  ...,
  failmissing = TRUE,
  names_to = "name",
  values_to = "value",
  zero_adjust = 0,
  onlyzeros = FALSE
)

Arguments

val_df

dataframe of values

compare_col

column or columns to use for grouping- needed if the baseline is one level in this column. Otherwise ignored

base_lev

value for baseline. Can be a level in compare_col, or a scalar, or a vector of length(unique(val_df$compare_col)). A dataframe would be useful but not yet implemented.

values_col

column containing the values. Can be character or tidyselect or bare names, though the last are fragile.

group_cols

character, columns to group the baselining by. Often spatial or env_obj or similar.

comp_fun

function to use for comparison comparing the first two arguments (difference and relative included here as wrappers for - and /). Can be character, or list-format, (both safe) or bare name (brittle)

...

additional arguments to comp_fun

failmissing

logical, default TRUE. Use tidyselect::any_of or tidyselect::all_of when values_col or compare_col are character. see selectcreator()

names_to

character, as in tidyr::pivot_longer(), used for auto-pivotting

values_to

character, as in tidyr::pivot_longer(), used for auto-pivotting

zero_adjust

numeric (default 0) or "auto", adjustment to data to avoid zeros by adding zero_adjust to abs(data), e.g shifting all data away from zero, either positively or negatively. Zeros themselves are shifted up or down randomly. Used for avoiding x/0, NaN, and Inf when relativiszing and taking logs, primarily. Auto shifts by 0.1*min(abs(data[data != 0])).

onlyzeros

logical, default FALSE. Should all values be adjusted away from zero (TRUE) or only adjust zero values (FALSE)?

Value

a tibble matching val_df (or a long version thereof if val_df is wide), with an added column for the reference level (named ref_values_col) and a column of the compared values (named comp_fun_values_col)