Re: [R] apply the function "factor" to multiple columns

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.be>
Date: Tue 31 May 2005 - 18:57:15 EST

you could try this way:

dat <- data.frame(V1 = factor(1:3), V2 = factor(1:3), V3 = factor(1:3))
dat[1:3] <- lapply(dat[1:3], factor, labels = c("None", "Low Impact", "MedHigh Imp"))
dat

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


>I have a case where I would like to change multiple columns
>containing
> numbers to factors. I can change each column one at a time as in:
>
>
>
> TEMP.FACT$EXPOS01<-factor(TEMP.FACT$EXPOS01,levels=c(1,2,3),labels=c("No
> ne","Low Impact","MedHigh Imp"))
>
> TEMP.FACT$EXPOS02<-factor(TEMP.FACT$EXPOS02,levels=c(1,2,3),labels=c("No
> ne","Low Impact","MedHigh Imp"))
>
> TEMP.FACT$EXPOS03<-factor(TEMP.FACT$EXPOS03,levels=c(1,2,3),labels=c("No
> ne","Low Impact","MedHigh Imp"))
>
>
>
>> summary(TEMP.FACT[,1:3])
>
> EXPOS01 EXPOS02 EXPOS03
>
> None :219 None :432 None :377
>
> Low Impact :428 Low Impact :248 Low Impact :297
>
> MedHigh Imp:108 MedHigh Imp: 77 MedHigh Imp: 83
>
> NA's : 25 NA's : 23 NA's : 23
>
>
>
> It would be much easier, however to use apply as in:
>
>
>
> TEMP.FACT
> [,1:3]<-apply(TEMP.FACT[,1:3],2,factor,labels=c("None","Low
> Impact","MedHigh Imp"))
>
>
>
> This appears to work (no error messages); however, this does not
> actually change the variables to factors. That is they are still
> treated as numbers:
>
>
>
>> summary(TEMP.FACT[,1:3])
>
> EXPOS01 EXPOS02 EXPOS03
>
> Min. : 1.000 Min. : 1.000 Min. : 1.000
>
> 1st Qu.: 1.000 1st Qu.: 1.000 1st Qu.: 1.000
>
> Median : 2.000 Median : 1.000 Median : 2.000
>
> Mean : 1.853 Mean : 1.531 Mean : 1.612
>
> 3rd Qu.: 2.000 3rd Qu.: 2.000 3rd Qu.: 2.000
>
> Max. : 3.000 Max. : 3.000 Max. : 3.000
>
> NA's :25.000 NA's :23.000 NA's :23.000
>
>
>
> Any ideas on how I could efficiently change a lot of columns to
> factors?
>
>
>
> Thanks,
>
>
>
> PB
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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 Tue May 31 19:04:39 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:16 EST