R/attrition_drop.R

Defines functions attrition_drop

Documented in attrition_drop

#' Replace values with NAs if respondent did not participate in a survey wave
#'
#' @export
#' @param imp_df Data frame from after imputation
#' @param wave The wave number
#' @param wave_indicator A logical vector. Each element should be
#'   TRUE if the respondent is in the wave and FALSE if not.
#' @return Updated \code{imp_df}.
#'
attrition_drop <- function(imp_df, wave, wave_indicator) {
  stopifnot(is.numeric(wave), is.logical(wave_indicator))
  sel <- grep(paste0("^q", wave), colnames(imp_df))
  imp_df[!wave_indicator, sel] <- NA
  imp_df
}
jgabry/RHhelpers documentation built on Feb. 22, 2024, 12:56 p.m.