check_assign_na | R Documentation |
Checks the specified output column in a data frame and assigns NA to its value depending on the missingness of a set of input columns.
If allow_missingness = TRUE
, the output column is set to NA only when all the specified input columns are NA.
If allow_missingness = FALSE
, the output column is set to NA when any of the input columns are NA.
This function is useful for propagating missingness from input variables to a derived output.
check_assign_na(data, output, input, allow_missingness = TRUE)
data |
tbl. Data frame containing the columns to be summarized. |
output |
character of length 1. The name of the first variable/column. |
input |
character. The name of the second variable/column. |
allow_missingness |
logical. Default set to TRUE. If TRUE, |
tbl. The input data frame with the output
column modified.
# Example data
dat <- tibble::tibble(
a = c(1, NA, 3),
b = c(NA, NA, 2),
c = c(1, 2, 3),
out = c(10, 11, 12)
)
# Assign NA to out when all of a and b are NA
check_assign_na(
dat,
output = "out", input = c("a", "b"), allow_missingness = TRUE
)
# Assign NA to out when any of a and b are NA
check_assign_na(
dat,
output = "out", input = c("a", "b"), allow_missingness = FALSE
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.