Re: [R] Remove columns from data-frame

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.ac.be>
Date: Mon 21 Mar 2005 - 21:38:27 EST

try this:

dat <- data.frame(y=rnorm(100), f1=sample(c("Yes", "No"), 100, TRUE), f2=rep("Yes", 100), f3=rep("No", 100))
#############
dat[!sapply(dat, function(x) all(x==x[1]))]

I hope it helps.

Best,
Dimitris



Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium

Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm

> Hello,
>
> I'm new to the list, hello everybody! :-)
>
>
> I have a question. I looked carefully through the documentation and
> googled, and found no answer, so I'm posting it here.
>
> Let's say I have a data frame with lots of columns (about 300),
> which
> are factors. Many columns-factors have only one level, for example
> all
> are "Yes" or all are "No".
>
> If I try to do a regression with that, I get an error message, that
> contrasts can be used only when number of factors is at least 2.
>
>> glm(mortality ~ ., family = binomial, data = ecdb_PROC88)
> Error in "contrasts<-"(`*tmp*`, value = "contr.treatment") :
> contrasts can be applied only to factors with 2 or more
> levels
>
>
> Of course, I could not include them in the model formula, but I
> don't
> feel like typing 150 descriptive variables.
>
> I would like to remove the one-level columns autimatically from the
> data
> frame. Is it possible?
>
> Regards,
> Maciej
>
> ______________________________________________
> R-help@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
>



R-help@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 Received on Mon Mar 21 21:40:46 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:51 EST