Drop Rows with missing values based on several columns

Screenshots are disfavored, and in the future, please use a reprex. See the FAQ.

But for simple cases, as a welcome, newcomers get a break.

make_fakes <- function() sample(c(1:100,NA),7)
set.seed(42)
(dat <- data.frame(c1 = make_fakes(),
                  c2 = make_fakes(),
                  c3 = make_fakes(),
                  c4 = make_fakes(),
                  c5 = make_fakes()))
#>   c1 c2 c3 c4 c5
#> 1 49 24 41 34 30
#> 2 65 71 89 92 43
#> 3 25 89 27  3 15
#> 4 74 37 36 58 22
#> 5 18 20 95 97 58
#> 6 NA 26  5 42  8
#> 7 47  3 84 24 36

dat[c(3,5),2:4] <- NA
dat[6,1] <- 12
dat
#>   c1 c2 c3 c4 c5
#> 1 49 24 41 34 30
#> 2 65 71 89 92 43
#> 3 25 NA NA NA 15
#> 4 74 37 36 58 22
#> 5 18 NA NA NA 58
#> 6 12 26  5 42  8
#> 7 47  3 84 24 36
dat |> rowSums(dat,na.rm = TRUE)
#> Warning in dims < 1L || dims > length(dn) - 1L: 'length(x) = 35 > 1' in coercion
#> to 'logical(1)'

#> Warning in dims < 1L || dims > length(dn) - 1L: 'length(x) = 35 > 1' in coercion
#> to 'logical(1)'
#> Error in rowSums(dat, dat, na.rm = TRUE): invalid 'dims'
dat[2:4]
#>   c2 c3 c4
#> 1 24 41 34
#> 2 71 89 92
#> 3 NA NA NA
#> 4 37 36 58
#> 5 NA NA NA
#> 6 26  5 42
#> 7  3 84 24
# remove rows with any of 2:4 containing NA
dat[-which(is.na(dat[2]) | is.na(dat[3]) | is.na(dat[4])),]
#>   c1 c2 c3 c4 c5
#> 1 49 24 41 34 30
#> 2 65 71 89 92 43
#> 4 74 37 36 58 22
#> 6 12 26  5 42  8
#> 7 47  3 84 24 36
# all of 2:4 containing NA
dat[-which(is.na(dat[2]) & is.na(dat[3]) & is.na(dat[4])),]
#>   c1 c2 c3 c4 c5
#> 1 49 24 41 34 30
#> 2 65 71 89 92 43
#> 4 74 37 36 58 22
#> 6 12 26  5 42  8
#> 7 47  3 84 24 36

Created on 2023-01-23 with reprex v2.0.2

2 Likes