coalesce_cols: Coalesce two columns

View source: R/utils-conv.R

coalesce_colsR Documentation

Coalesce two columns

Description

Coalesce two columns

Usage

coalesce_cols(df, coalesce_in_cols, coalesce_out_cols)

Arguments

df

a data table with the desired columns to coalesce

coalesce_in_cols

a character vector of column names representing the columns to be coalesced into and that take precedence

coalesce_out_cols

a character vector of column names representing the columns to be coalesced out of

Details

This function modifies by reference. The coalesce_in_cols columns will take precedence over the values in the coalesce_out_cols columns if both columns contain data. The data is coalesced into the coalesce_in_cols so that the data in the coalesce_out_cols is not altered. The coalesce_out_cols columns are not removed.

If df does not contain any coalesce_out_cols columns, no changes to df are made. If df contains coalesce_out_cols columns but no coalesce_in_cols, the coalesce_out_cols columns are simply renamed to their counterpart coalesce_in_cols columns. For this reason, coalesce_in_cols and coalesce_out_cols must be the same length and matching indices between the two vectors represent respective columns to be coalesced into and out of.

Value

a data table

Examples

x <- data.table::data.table(a = c(NA, TRUE, FALSE), b = c(NA, TRUE, FALSE),
c = c(NA, TRUE, FALSE), d = c(NA, TRUE, FALSE),
A = c(TRUE, NA, TRUE), B = c(TRUE, NA, TRUE))
coalesce_in_cols <- c("A","B","C","D")
coalesce_out_cols <- c("a","b","c","d")
nsqipr:::coalesce_cols(x, coalesce_in_cols, coalesce_out_cols)
x


dylanrussellmd/nsqipr documentation built on Oct. 13, 2023, 11:01 a.m.