add_missing_column: Add missing columns to a data frame

View source: R/add_missing_column.R

add_missing_columnR Documentation

Add missing columns to a data frame

Description

add_missing_column() adds one or more columns to an existing data frame only if they do not already exist.

This is a simple wrapper around add_column().

Usage

add_missing_column(
  .data,
  ...,
  .before = NULL,
  .after = NULL,
  .name_repair = c("check_unique", "unique", "universal", "minimal")
)

Arguments

.data

Data frame to append to.

...

<dynamic-dots> Name-value pairs, passed on to tibble(). All values must have the same size of .data or size 1.

.before, .after

One-based column index or column name where to add the new columns, default: after last column.

.name_repair

Treatment of problematic column names:

  • "minimal": No name repair or checks, beyond basic existence,

  • "unique": Make sure names are unique and not empty,

  • "check_unique": (default value), no name repair, but check they are unique,

  • "universal": Make the names unique and syntactic

  • a function: apply custom name repair (e.g., .name_repair = make.names for names in the style of base R).

  • A purrr-style anonymous function, see rlang::as_function()

This argument is passed on as repair to vctrs::vec_as_names(). See there for more details on these terms and the strategies used to enforce them.

See Also

add_column()

Examples

# add new columns x and y
add_missing_column(mtcars, x = 1, y = NA)

# add new columns from a named vector
new_cols <- c(x = 1, y = NA)
add_missing_column(mtcars, !!!new_cols)


Technomics/costmisc documentation built on Nov. 12, 2024, 8:05 a.m.