Remove Rows Containing Missing Values

drop_na() drops rows that contain NA values in selected columns.

library(tidyr)library(dplyr)
df <- tibble(id = c(1, 2, NA, 4), first_name = c("Alice", "Bob", NA, "David"), last_name = c("Smith", "Johnson", NA, "Miller"), age = c(25, NA, 35, 40))df

Output:

# A tibble: 4 × 4
id first_name last_name age
<dbl> <chr> <chr> <dbl>
1 1 Alice Smith 25
2 2 Bob Johnson NA
3 NA <NA> <NA> 35
4 4 David Miller 40

Remove rows containing missing values NA. Only rows not containing NA are retained in the output.

df %>% drop_na()

Output:

# A tibble: 2 × 4
id first_name last_name age
<dbl> <chr> <chr> <dbl>
1 1 Alice Smith 25
2 4 David Miller 40

Remove rows that contain NA values in the id and first_name column.

df %>% drop_na(id, first_name)

Output:

# A tibble: 3 × 4
id first_name last_name age
<dbl> <chr> <chr> <dbl>
1 1 Alice Smith 25
2 2 Bob Johnson NA
3 4 David Miller 40

Selection helpers can be used to conveniently select a block of columns. E.g., here we remove rows that contain NA values in numeric columns.

df %>% drop_na(where(is.numeric))

Output:

# A tibble: 2 × 4
id first_name last_name age
<dbl> <chr> <chr> <dbl>
1 1 Alice Smith 25
2 4 David Miller 40