coalesce_cols | R Documentation |
Coalesce two columns
coalesce_cols(df, coalesce_in_cols, coalesce_out_cols)
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 |
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.
a data table
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.