Drop Rows with missing values based on several columns

Hi, try this.

library(tidyverse)

# fake data
df <- tibble(c1 = sample(1:100, 7),
             c2 = c(1, 2, NA, 2, NA, 5, NA),
             c3 = c(1, 2, NA, 2, NA, 6, 4),
             c4 = c(1, 2, NA, 2, NA, 7, NA),
             c5 = sample(1:100, 7)) 


df %>% 
  filter(if_any(c2:c4, ~ !is.na(.)))


# # A tibble: 5 x 5
# c1    c2    c3    c4    c5
# <int> <dbl> <dbl> <dbl> <int>
# 1    57     1     1     1    80
# 2    86     2     2     2    95
# 3    26     2     2     2    43
# 4    95     5     6     7    71
# 5    58    NA     4    NA    59

Also, next time please provide a reproducible example instead of a screenshot.

1 Like