[R] Odp: having problems with factor()

Petr PIKAL petr.pikal at precheza.cz
Fri Aug 10 14:02:21 CEST 2007


Hi
r-help-bounces at stat.math.ethz.ch napsal dne 10.08.2007 13:41:53:

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

Hm, maybe some of the values are missing

> num<-sample(1:3, 10, replace=T)
> num
 [1] 1 3 1 2 3 3 1 3 3 3
> factor(num, labels=c("O", "I", "II"))
 [1] O  II O  I  II II O  II II II
Levels: O I II

> factor(num, labels=c("O", "I", "II", "III"))
Error in factor(num, labels = c("O", "I", "II", "III")) : 
        invalid labels; length 4 should be 1 or 3
>

try

table(df$area)
to see what level you really have

Regards
Petr


> 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
> 
> 
>       ___________________________________________________________
> 
> now.
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list