Delete cases with set amount of missing values

delete_na(df, n = ncol(df) - 1)

Arguments

df

A data.frame,

n

Number of NAs allowed, defaults to ncol(df) - 1.

Value

A filtered version of the input data.frame.

Note

Adapted from http://stackoverflow.com/a/30461945/409362.

Examples

set.seed(1445) dat <- data.frame( x = sample(c(1:15, NA, NA), 15), y = sample(c(1:15, NA, NA), 15), z = sample(c(1:15, NA, NA), 15) ) dat
#> x y z #> 1 11 12 7 #> 2 10 6 2 #> 3 8 5 5 #> 4 4 8 15 #> 5 15 7 NA #> 6 NA 9 12 #> 7 2 4 8 #> 8 3 15 11 #> 9 14 2 NA #> 10 9 14 4 #> 11 12 13 1 #> 12 1 3 6 #> 13 5 11 14 #> 14 6 10 10 #> 15 7 NA 13
# No NsA per row allowed delete_na(dat, 0)
#> x y z #> 1 11 12 7 #> 2 10 6 2 #> 3 8 5 5 #> 4 4 8 15 #> 7 2 4 8 #> 8 3 15 11 #> 10 9 14 4 #> 11 12 13 1 #> 12 1 3 6 #> 13 5 11 14 #> 14 6 10 10
# One NA per row allowed delete_na(dat, 1)
#> x y z #> 1 11 12 7 #> 2 10 6 2 #> 3 8 5 5 #> 4 4 8 15 #> 5 15 7 NA #> 6 NA 9 12 #> 7 2 4 8 #> 8 3 15 11 #> 9 14 2 NA #> 10 9 14 4 #> 11 12 13 1 #> 12 1 3 6 #> 13 5 11 14 #> 14 6 10 10 #> 15 7 NA 13