Skip to contents

convert_f_defaults() combine the dplyr::rename() way of working and with the tibble::add_column() to add columns with default values in case they don't exist after renaming data.

Usage

convert_f_defaults(.data, ..., .def_col_val = c(), .use_dots = TRUE)

Arguments

.data

A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). See Methods, below, for more details.

...

For rename(): <tidy-select> Use new_name = old_name to rename selected variables.

For rename_with(): additional arguments passed onto .fn.

.def_col_val

Named vector with columns with default values if none exist after rename.

.use_dots

Should a dot prefix be added to renamed variables? This will allow swapping of columns.

Value

An object of the same type as .data. The output has the following properties:

  • Rows are not affected.

  • Column names are changed.

  • Column order is the same as that of the function call.

Details

The objective of using .use_dots is to be able to swap columns which, by default, is not allowed by the dplyr::rename() function. The same behavior can be replicated by simply using the dplyr::select(), however, the select evaluation allows much more flexibility so that unexpected results could be obtained. Despite this, a future implementation will consider this form of execution to allow renaming the same column to multiple ones (i.e. extend dataframe extension).

Examples


df <- tibble::tibble(x = 1, y = 2, z = 3)

# Rename columns
df <- tibble::tibble(x = 1, y = 2)
convert_f_defaults(
    .data = df,
    new_x = x,
    new_y = y,
    new_z = NULL,
    .def_col_val = c(new_z = 3)
)
#> # A tibble: 1 × 3
#>   new_x new_y new_z
#>   <dbl> <dbl> <dbl>
#> 1     1     2     3