Skip to contents

ifdo() replaces imputations that meet a condition. It is meant to be used inside the post argument of mice(), where it acts on the imputations of the current variable in the current iteration.

Usage

ifdo(cond, action)

Arguments

cond

A condition, or several conditions combined with c(), e.g. c(Ozone < 1, Ozone > 200). Conditions are expressions in terms of the variables in the data.

action

The replacement values, one for each condition, combined with c() if there is more than one, e.g. c(1, 200).

Value

Called for its side effect: it updates the imputations of the current variable. Returns NULL invisibly.

Details

Each condition is evaluated against the data as they are after the imputation of the current variable, so the current variable holds its new imputations. Expressions may refer to any variable in the data. For the rows where condition k is TRUE, the imputations are replaced by action k. An action can be a constant, or an expression that is evaluated for those rows, such as Ozone / 2. All conditions are evaluated before any replacement is made, so a later action does not change which rows meet a later condition. If a row meets more than one condition, the last action wins. Rows where a condition is NA are left unchanged.

ifdo() works wherever mice() applies post, that is, for variables imputed by a univariate method. It does nothing useful outside post.

References

Van Buuren, S. (2018). Flexible Imputation of Missing Data. Second Edition. Chapman & Hall/CRC. Section 6.4.6.

See also

Author

Stef van Buuren, 2012, 2026

Examples

post <- make.post(airquality[, 1:2])
post["Ozone"] <- "ifdo(c(Ozone < 1, Ozone > 200), c(1, 200))"
imp <- mice(airquality[, 1:2],
  method = "norm.nob", m = 1, maxit = 1,
  seed = 1, post = post, print = FALSE
)
range(imp$imp$Ozone)
#> [1]   1.0000 115.4566

# same result as
post["Ozone"] <- "imp[[j]][, i] <- squeeze(imp[[j]][, i], c(1, 200))"