Skip to contents

Applies a series of standard data transformations including renaming, recoding, filtering and variable selection.

Usage

transform_with_lists(
  data_xx,
  filters_ls = list(),
  recode_ls = list(),
  rename_ls = list(),
  output_1L_chr = c("tbl_df", "data.frame", "data.table"),
  select_chr = character(),
  transformation_1_fn = identity,
  transformation_2_fn = identity
)

Arguments

data_xx

Input dataset.

filters_ls

Named list of filtering criteria.

recode_ls

Named list of recoding maps.

rename_ls

Named list mapping new names to existing variable names.

output_1L_chr

Output type. One of "tbl_df", "data.frame" or "data.table".

select_chr

Variables to retain.

transformation_1_fn

Function applied before variable selection.

transformation_2_fn

Function applied after variable selection.

Value

A transformed dataset.

Details

This function is designed to reduce repetitive data-preparation code when constructing lookup tables and contingency tables.

Transformations are applied in the following order:

  1. Rename variables.

  2. Recode values.

  3. Filter observations.

  4. Apply transformation_1_fn.

  5. Select variables.

  6. Apply transformation_2_fn.

  7. Convert to the requested output format.

See also

Examples

transform_with_lists(
  mtcars,
  rename_ls = list(
    mpg_new = "mpg"
  ),
  select_chr = c(
    "mpg_new",
    "cyl"
  )
)
#> # A tibble: 32 × 2
#>    mpg_new   cyl
#>      <dbl> <dbl>
#>  1    21       6
#>  2    21       6
#>  3    22.8     4
#>  4    21.4     6
#>  5    18.7     8
#>  6    18.1     6
#>  7    14.3     8
#>  8    24.4     4
#>  9    22.8     4
#> 10    19.2     6
#> # ℹ 22 more rows