Removing NA Values From DataFrame: Efficient Column-Based Approach Using Dplyr

Here is a high-quality code snippet that accomplishes the task:

library(dplyr)

df %>% 
  filter_at(.cols = function(x) sum(is.na(x)) == min(sum(is.na(x))) & !is.na(names(x)), ~ 1) %>% 
  drop_na()

This code first identifies the columns with minimum number of NA values using filter_at. It then drops rows from these columns that contain NA values.


Last modified on 2024-09-29