[R] Question about factor that is numeric, in aov()

Adrian Dusa dusa.adrian at gmail.com
Sun May 9 19:44:48 CEST 2010

Dear Ravi,

On Sunday 09 May 2010, Ravi Kulkarni wrote:
> I notice something curious about how aov() treats a numeric factor:

In R, there is no such thing as a "numeric factor". A numeric vector is not a 
factor unless declared as such.

> "score" is a dependent variable and "group" is a factor in a one-way ANOVA.
> But "group" contains numeric codes and is not a factor (checked with
> is.factor). An ANOVA done using:
> > aov(score~factor(group), data=mydata)
> gives the right answers. But
> > aov(score~group, data=mydata)
> also produces an ANOVA table, with incorrect entries. My question is: what
> exactly is R doing when I did not specify that "group" was a factor?

The entries _are_ correct, because "group" is numeric.
From the help of aov():


     This provides a wrapper to ‘lm’ for fitting linear models to
     balanced or unbalanced experimental designs.

So aov() calls lm(), where it is mighty important whether "group" is numeric 
or factor. There are both in your mind, but in R you have to declare it as 
factor in order to treat it as such...

I hope this helps,

Adrian Dusa
Romanian Social Data Archive
1, Schitu Magureanu Bd.
050025 Bucharest sector 5
Tel.:+40 21 3126618 \
     +40 21 3120210 / int.101
Fax: +40 21 3158391

More information about the R-help mailing list