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/
Kjetil Brinchmann Halvorsen
2004-Sep-17 23:17 UTC
[R] Removing constants from a data frame
David Forrest wrote:>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 > >what about defing is.constant as is.constant <- function(x) { if (is.numeric(x)) identical(min(x), max(x)) else FALSE } Kjetil halvorsen -- Kjetil Halvorsen. Peace is the most effective weapon of mass construction. -- Mahdi Elmandjra
On 17 Sep 2004 at 19:17, Kjetil Brinchmann Halvorsen wrote:> David Forrest wrote: > > >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 > > > > > what about defing is.constant as > is.constant <- function(x) { > if (is.numeric(x)) identical(min(x), max(x)) > else > FALSE } > > Kjetil halvorsenHi Maybe this will do it> fff<-function(a) if(is.numeric(a)) diff(range(a, na.rm=T))==0 else FALSE > x[,!mapply(fff,x)]v1 v2 v4 v5 1 1 2 A Z 2 2 4 B Z 3 3 NA C Z 4 4 7 D Z>Cheers Petr> > -- > > Kjetil Halvorsen. > > Peace is the most effective weapon of mass construction. > -- Mahdi Elmandjra > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlPetr Pikal petr.pikal at precheza.cz
> From: Kjetil Brinchmann Halvorsen > > David Forrest wrote: > > >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 > > > > > what about defing is.constant as > is.constant <- function(x) { > if (is.numeric(x)) identical(min(x), > max(x)) else > FALSE }identical() is probably not the safest thing to use:> x <- c(1, 2, NA) > is.constant(x)[1] TRUE For data such as c(1, 1, 1, NA), I should think the safest answer should be NA, because one really doesn't know whether that last number is 1 or not. Andy> -- > Kjetil Halvorsen. > > Peace is the most effective weapon of mass construction. > -- Mahdi Elmandjra > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >