convert_f_defaults {decoupleR} | R Documentation

## Rename columns and add defaults values if column not present

### Description

`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.

### 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).

### 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.

### 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)
    )

[Package decoupleR version 2.0.1 Index]