简体   繁体   中英

Identify a list of (or remove) variables in a data frame that are completely empty (NAs)

I have a very large data frame with numerous variables that are completely empty (NAs). My goal is to remove these variables. I want to exclude empty variables, not missing values. This seems like a very basic question but I cannot figure it out.

#sample data
A<-rbinom(100,1,1/2)
B<-rbinom(100,1,1/2)
C<-NA
D<-NA
df<-as.data.frame(cbind((1:100),A,B,C,D))
df<-as.data.frame(lapply(df, function(x) 
               "is.na<-"(x, sample(seq(x), floor(length(x) * runif(1, 0, .2))))))
Hmisc::describe(df)

I can make a list of these variables using Hmisc::describe(), but I can't figure out how to extract or use this list.

Try this:

df[,!sapply(df,function(x) all(is.na(x)))]

or, to be extra safe:

df[,!sapply(df,function(x) all(is.na(x))),drop = FALSE]

Try:

apply(df,2,function(x) sum(!is.na(x)))

All variables with only NA will have sum 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM