introduce: Introduce non-existing columns

View source: R/vars.R

introduceR Documentation

Introduce non-existing columns

Description

Works like dplyr::mutate() but without changing existing columns, but only adding new ones. Useful to add possibly missing columns with default values.

Usage

introduce(.data, ...)

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.

...

<data-masking> Name-value pairs. The name gives the name of the column in the output.

The value can be:

  • A vector of length 1, which will be recycled to the correct length.

  • A vector the same length as the current group (or the whole data frame if ungrouped).

  • NULL, to remove the column.

  • A data frame or tibble, to create multiple columns in the output.

Examples

# ensure columns "y" and "z" exist
tibble::tibble(x=1:3) %>%
 introduce(y="a", z=paste0(y, dplyr::row_number()))
# ensure columns "y" and "z" exist, but do not overwrite "y"
tibble::tibble(x=1:3, y=c("c", "d", "e")) %>%
 introduce(y="a", z=paste0(y, dplyr::row_number()))

thackl/gggenomes documentation built on March 10, 2024, 7:26 a.m.