Answer by Airdjan for Remove columns from dataframe where ALL values are NA
library(dplyr)# create a sample data framedf <- data.frame(x = c(1, 2, NA, 4), y = c(NA, NA, NA, NA), z = c(6, 7, NA, 9))# remove columns with all NAsdf <- df %>% select_if(~!all(is.na(.)))
View ArticleAnswer by Grant for Remove columns from dataframe where ALL values are NA
An old question, but I think we can update @mnel's nice answer with a simpler data.table solution:DT[, .SD, .SDcols = \(x) !all(is.na(x))](I'm using the new \(x) lambda function syntax available in...
View ArticleAnswer by J. Lucas McKay for Remove columns from dataframe where ALL values...
janitor::remove_constant() does this very nicely.
View ArticleAnswer by user1211719 for Remove columns from dataframe where ALL values are NA
From my experience of having trouble applying previous answers, I have found that I needed to modify their approach in order to achieve what the question here is:How to get rid of columns where for ALL...
View ArticleAnswer by ok1more for Remove columns from dataframe where ALL values are NA
You can use Janitor package remove_emptylibrary(janitor)df %>% remove_empty(c("rows", "cols")) #select either row or cols or bothAlso, Another dplyr approach library(dplyr) df %>%...
View ArticleAnswer by tmfmnk for Remove columns from dataframe where ALL values are NA
A handy base R option could be colMeans():df[, colMeans(is.na(df)) != 1]
View ArticleAnswer by AlexB for Remove columns from dataframe where ALL values are NA
Another options with purrr package:library(dplyr)df <- data.frame(a = NA, b = seq(1:5), c = c(rep(1, 4), NA))df %>% purrr::discard(~all(is.na(.)))df %>% purrr::keep(~!all(is.na(.)))
View ArticleAnswer by André.B for Remove columns from dataframe where ALL values are NA
Late to the game but you can also use the janitor package. This function will remove columns which are all NA, and can be changed to remove rows that are all NA as well.df <-...
View ArticleAnswer by zack for Remove columns from dataframe where ALL values are NA
UpdateYou can now use select with the where selection helper. select_if is superceded, but still functional as of dplyr 1.0.2. (thanks to @mcstrother for bringing this to attention).library(dplyr)temp...
View ArticleAnswer by jpmorris for Remove columns from dataframe where ALL values are NA
df[sapply(df, function(x) all(is.na(x)))] <- NULL
View ArticleAnswer by Luis M. Nieves for Remove columns from dataframe where ALL values...
I hope this may also help. It could be made into a single command, but I found it easier for me to read by dividing it in two commands. I made a function with the following instruction and worked...
View ArticleAnswer by mnel for Remove columns from dataframe where ALL values are NA
The two approaches offered thus far fail with large data sets as (amongst other memory issues) they create is.na(df), which will be an object the same size as df.Here are two approaches that are more...
View ArticleAnswer by mropa for Remove columns from dataframe where ALL values are NA
Another way would be to use the apply() function.If you have the data.framedf <- data.frame (var1 = c(1:7,NA), var2 = c(1,2,1,3,4,NA,NA,9), var3 = c(NA) )then you can use apply() to see which...
View ArticleAnswer by teucer for Remove columns from dataframe where ALL values are NA
Try this:df <- df[,colSums(is.na(df))<nrow(df)]
View ArticleRemove columns from dataframe where ALL values are NA
I have a data frame where some of the columns contain NA values.How can I remove columns where all rows contain NA values?
View Article
More Pages to Explore .....