Learn R Programming

ABCDscores (version 6.1.0)

check_assign_na: Check an output field and assign NA when input variables all have NAs

Description

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.

Usage

check_assign_na(data, output, input, allow_missingness = TRUE)

Value

tbl. The input data frame with the output column modified.

Arguments

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, output field is set to NA only when ALL the fields in input have missingness. If FALSE, output is set to NA when ANY of the input fields have missingness.

Examples

Run this code
# 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
)

Run the code above in your browser using DataLab