[R] Remove columns from data-frame

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.ac.be
Mon Mar 21 11:38:27 CET 2005


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

----- Original Message ----- 
From: "Maciej Bliziński" <m.blizinski at wsisiz.edu.pl>
To: <r-help at stat.math.ethz.ch>
Sent: Monday, March 21, 2005 11:12 AM
Subject: [R] Remove columns from data-frame


> 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 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
>




More information about the R-help mailing list