# [R] having problems with factor()

Rainer Hurling rhurlin at gwdg.de
Fri Aug 10 14:11:37 CEST 2007

```I am afraid the above example will not work. In original dataset of
Jabez Wilson numerical range is from 0..7.

So try this one:

df<-as.factor(c("0","I","II","III","IV","V","VI","VII")[df\$area+1])

Hope this is what you want,
Rainer

Henrique Dallazuanna schrieb:
> Hi,
>
> df
>     ht area
> 1  320    3
> 2  410    4
> 3  230    2
> 4  360    3
> 5  126    1
> 6  280    2
> 7  260    2
> 8  280    2
> 9  280    2
> 10 260    2
>
> df\$area <- as.factor(df\$area)
> levels(df\$area) <- c("I", "II", "III", "IV")
>
>
> On 10/08/07, Jabez Wilson <jabezwuk at yahoo.co.uk> wrote:
>> Dear R Help,
>> I have a set of data of heights of trees described by area that they
>> are in. The areas are numerical (0 to 7).
>>
>>     ht    area
>> 1   320   3
>> 2   410   4
>> 3   230   2
>> 4   360   3
>> 5   126   1
>> 6   280   2
>> 7   260   2
>> 8   280   2
>> 9   280   2
>> 10  260   2
>> .......
>> 180 450   4
>> 181  90   1
>> 182 120   1
>> 183 440   4
>> 184 210   2
>> 185 330   3
>> 186 210   2
>> 187 100   1
>> 188   0   0
>>
>> I want to convert the area column values to factors, to do an anova.
>> However, if I use:
>>
>> df\$areaf <- factor(df\$area,
>> labels=c("0","I","II","III","IV","V","VI","VII"))
>>
>> it gives the following message:
>>
>> Error in factor(df\$area, labels = c("0", "I", "II", "III", "IV", "V",
>> "VI",  :
>>         invalid labels; length 8 should be 1 or 7
>>
>> Can anyone help?
>>
>> Jabez

```