[R] Warning message

Thomas Lumley tlumley at u.washington.edu
Thu Jul 25 17:34:40 CEST 2002


On Thu, 25 Jul 2002, Jim Lemon wrote:

> Hi,
>
> Without knowing what the object contains, it's hard to tell. Factor
> objects are numeric mode but generally screw up when treated as numbers.
> See what happens when you enter:
>
> class(mensuel$VARIANCE.PRIX)
>
> and you may be able to get away with:
>
> mean(unclass(mensuel$VARIANCE.PRIX))
>

More likely you want

mean( as.numeric(as.character(mensuel$VARIANCE.PRIX)))

Using unclass() or as.numeric() directly on a factor gives you the
underlying codes, but in a case like this people usually want the numeric
version of the levels, eg
> a<-factor(1:10,labels=10:1)
> a
 [1] 10 9  8  7  6  5  4  3  2  1
Levels:  10 9 8 7 6 5 4 3 2 1
> unclass(a)
 [1]  1  2  3  4  5  6  7  8  9 10
attr(,"levels")
 [1] "10" "9"  "8"  "7"  "6"  "5"  "4"  "3"  "2"  "1"
>  as.numeric(a)
 [1]  1  2  3  4  5  6  7  8  9 10
> as.numeric(as.character(a))
 [1] 10  9  8  7  6  5  4  3  2  1

This is actually a FAQ.

	-thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list