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


