[R] Removing constants from a data frame

David Forrest drf5n at maplepark.com
Fri Sep 17 23:11:24 CEST 2004


Suppose I have

x<-data.frame(v1=1:4, v2=c(2,4,NA,7), v3=rep(1,4),
     v4=LETTERS[1:4],v5=rep('Z',4))

or a much larger frame, and I wish to test for and remove the constant
numeric columns.

I made:

   is.constant<-function(x){identical(min(x),max(x))}

and
   apply(x,2,is.constant) # Works for numerics
   x[,-which(apply(x,2,is.constant))]

I'd really like to be able to delete the constant columns without losing
my non-numerics.  Ignoring the character columns would be OK.

Any suggestions?

Dave
-- 
 Dave Forrest
 drf at vims.edu                                    (804)684-7900w
 drf5n at maplepark.com                             (804)642-0662h
                                   http://maplepark.com/~drf5n/




More information about the R-help mailing list