Skip to content
Prev 304585 / 398503 Next

Identifying and Removing NA Columns and factor Columns with more than x Levels

Hi Bert,

Thanks! These worked perfectly.

mydata<-mydata[,!(i1)]

Dan

-----Original Message-----
From: Bert Gunter [mailto:gunter.berton at gene.com] 
Sent: Thursday, August 30, 2012 8:54 AM
To: Lopez, Dan
Cc: R help (r-help at r-project.org)
Subject: Re: [R] Identifying and Removing NA Columns and factor Columns with more than x Levels

If d is your data frame

i1 <- sapply(d,function(x)is.factor(x)&&length(levels(x))>31)
## a vector of length ncol(d) that is TRUE only for factor columns with >31 levels

i2 >- sapply(d,function(x)any(is.na(x)))
## You can figure it out.

-- Bert
On Thu, Aug 30, 2012 at 8:38 AM, Lopez, Dan <lopez235 at llnl.gov> wrote: