Skip to contents

This function takes a named list of purr-style lambdas where names are the names of the columns in the data frame that must be transformed. NOTE: the columns are overridden, not appended.

Usage

transform_columns(df, transf_list)

Arguments

df

The data frame on which transformations should be operated

transf_list

A named list of purrr-style lambdas, where names are column names the function should be applied to.

Value

A data frame with transformed columns

Details

Lambdas provided in input must be transformations, aka functions that take in input a vector and return a vector of the same length as the input.

If the input transformation list contains column names that are not present in the input data frame, they are simply ignored.

Examples

df <- tibble::tribble(
    ~A, ~B, ~C, ~D,
    1, 2, "a", "aa",
    3, 4, "b", "bb",
    5, 6, "c", "cc"
)
lambdas <- list(A = ~ .x + 1, B = ~ .x + 2, C = ~ stringr::str_to_upper(.x))
transform_columns(df, lambdas)
#> # A tibble: 3 × 4
#>       A     B C     D    
#>   <dbl> <dbl> <chr> <chr>
#> 1     2     4 A     aa   
#> 2     4     6 B     bb   
#> 3     6     8 C     cc