Apply random or systematic missingness to existing data according to recipe

inject_nas(tbl, recipe)

Arguments

tbl

data.frame/tibble. The already generated data to inject missingness into

recipe

tibble. A recipe for generating missingness positions

Value

tbl, with missingness injected into it as laid out by recipe

Details

Unlike in data-generation recipes, the func column in a missingness recipe must return a logical vector of length n or an n x k logical matrix, where n is the number of rows in .df and k is the number of variables listed in this row of the recipe. A vector is only allowed when only one variable is listed in the recipe row (ie k=1). TRUE in the return value indicates that position in the column being processed should be set to missing (NA), while FALSE indicates the value already there should remain unchanged.

Examples

library(tibble) missrec <- tibble(variables = "wt", func = list(function(.df) rep(c(TRUE, FALSE), times = c(3, NROW(.df) - 3))), func_args = list(NULL)) newdat <- inject_nas(mtcars, missrec) head(newdat)
#> mpg cyl disp hp drat wt qsec vs am gear carb #> Mazda RX4 21.0 6 160 110 3.90 NA 16.46 0 1 4 4 #> Mazda RX4 Wag 21.0 6 160 110 3.90 NA 17.02 0 1 4 4 #> Datsun 710 22.8 4 108 93 3.85 NA 18.61 1 1 4 1 #> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 #> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 #> Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1