# A seemingly simple ANOVA problem in R: Factor naming changes result output

I suspect this is an easy question but I don't know what the answer is and would appreciate any help.

I have a dataset (UWb) like this...

  subject whitelvl stainlvl bwhite  aged stained fresh wellbeing
1       2     1       1     80.75  10.50   85.50 14.75     16.50
2       2     2       2     72.25  71.50   54.25 40.00     28.50
3       2     3       6     67.50  58.50   48.50 39.25     33.75
4       2     1       1     66.00  64.00    0.25 72.75     69.50


When I do an ANOVA with this I get a normal output, like the following

                    Df Sum Sq Mean Sq F value    Pr(>F)
Whitelvl             1  95755   95755 752.265 < 2.2e-16 ***
Stainlvl             1  77918   77918 612.135 < 2.2e-16 ***
Whitelvl:Stainlvl    1   1745    1745  13.712 0.0002182 ***
Residuals         2246 285890     127
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


However, in order to make this easier for others to read I have changed the names of the levels of the two factors: whitelvl and stainlvl, with the following code...

UWb$Whitelvl<-factor(c("1","2","3")) levels(UWb$Whitelvl)
UWb$Stainlvl<-factor(c("1","2","6")) levels(UWb$Stainlvl)
UWb$Whitelvl<-revalue(UWb$Whitelvl, c("1"="Bright", "2"="Dull", "3"="Grey"))
UWb$Stainlvl<-revalue(UWb$Stainlvl, c("1"="None","2"="Light","6"="Strong"))


This creates the following changes...

  subject whitelvl stainlvl bwhite  aged stained fresh wellbeing
1       2   Bright     None  80.75 10.50   85.50 14.75     16.50
2       2     Dull    Light  72.25 71.50   54.25 40.00     28.50
3       2     Grey   Strong  67.50 58.50   48.50 39.25     33.75
4       2   Bright     None  66.00 64.00    0.25 72.75     69.50


However when I do an anova with the same code as above...

whiteness.aov = aov(BWhite ~ Whitelvl*Stainlvl, data=UWb)
summary(whiteness.aov)


I get the following...

              Df Sum Sq Mean Sq F value Pr(>F)
whitelvl       2      0     0.0       0      1
Residuals   2247 461309   205.3


I presume its something about turning the conditions into factors, but that is what they are so why is this causing R a problem? Is there a way to rename and get the same results as the first ANOVA?

Ah. Simple.

UWb$Whitelvl<-factor(c("1","2","3")) UWb$Stainlvl<-factor(c("1","2","6"))


The code above replaces whitelvl and stainlvl with two perfectly correlated sequences of factors.

In R, if you attempt to insert a shorter vector in a longer one, if the longer is a multiplicity of the shorter, you don't get an error. By a freak chance, the number of rows in Uwb is divisible by three, so you don't get an error.

I guess you meant

UWb$Whitelvl<-factor(Uwb$Whitelvl, levels=c("1","2","3"))
UWb$Stainlvl<-factor(Uwb$Stainlvl, levels=c("1","2","6"))

• It seems so obvious now, which is always the sign of a great answer! Thank you so much. – Comte Mar 31 '17 at 13:52