# [R] apply the function "factor" to multiple columns

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Tue May 31 10:57:15 CEST 2005

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

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: "Bliese, Paul D LTC USAMH" <paul.bliese at us.army.mil>
To: <r-help at stat.math.ethz.ch>
Sent: Tuesday, May 31, 2005 10:42 AM
Subject: [R] apply the function "factor" to multiple columns

>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 at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help