Quantcast
Channel: Remove columns from dataframe where ALL values are NA - Stack Overflow
Browsing all 15 articles
Browse latest View live

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 Article



Answer 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 Article

Answer by J. Lucas McKay for Remove columns from dataframe where ALL values...

janitor::remove_constant() does this very nicely.

View Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article


Answer by jpmorris for Remove columns from dataframe where ALL values are NA

df[sapply(df, function(x) all(is.na(x)))] <- NULL

View Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer by teucer for Remove columns from dataframe where ALL values are NA

Try this:df <- df[,colSums(is.na(df))<nrow(df)]

View Article

Remove 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

Browsing all 15 articles
Browse latest View live




Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>