Site icon R-bloggers

Drop columns based on NAs percentage in R

[This article was first published on R | TypeThePipe, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Are you developing an automated exploration tool? Here we propose some alternatives to drop columns with high percentage of NAs.

In this previous tip we talk about BaseR vs Tidy & Purrr counting NAs performance.

Not leaving the pipeflow. How much does it cost?;) It depends on the NA distribution between features and its number, but not much that a few nanoseconds in small and big datasets

# library(microbenchmark) You can benchmark them in small and big datasets
library(tidyverse)

airquality %>% select_if(~mean(is.na(.)) < 0.2)

airquality %>% select(which(colMeans(is.na(.)) < 0.2))

airquality[lapply(airquality, function(x) mean(is.na(x))) < 0.2]


Soooo what’s your choice??


To leave a comment for the author, please follow the link and comment on their blog: R | TypeThePipe.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.