# [R] adonis (vegan package) and subsetted factors

tyler tyler.smith at mail.mcgill.ca
Thu Apr 10 16:18:56 CEST 2008

```Hi,

I'm trying to use adonis on a subset of data from a dataframe. The
actual data is in columns 5:118, and the first four columns are various
factors. There are 3 levels of the factor Habitat, and I want to examine
differences among only two of them. So I started with:

> CoastNear = subset(gel_data, Habitat != "I")

The resulting data.frame has three levels for Habitat, but only two of
those levels have any records. Then I run:

+ method='jaccard')

Call:
adonis(formula = CoastNear[, 5:118] ~ Habitat, data = CoastNear,
permutations = 1000, method = "jaccard")

Df  SumsOfSqs    MeanSqs    F.Model     R2 Pr(>F)
Habitat    2.0000000  0.0092966  0.0046483  2.0549327 0.0707  0.005
Residuals 54.0000000  0.1221491  0.0022620            0.9293
Total     56.0000000  0.1314457                       1.0000

This appears to be wrong - with only two Habitat levels I should only
have 1 Df, shouldn't I? I checked by forcibly excising the third factor
level:

> CoastNear\$Habitat <- as.factor(as.character(CoastNear\$Habitat))
+ method='jaccard')

Call:
adonis(formula = CoastNear[, 5:118] ~ Habitat, data = CoastNear,
permutations = 1000, method = "jaccard")

Df  SumsOfSqs    MeanSqs    F.Model     R2 Pr(>F)
Habitat    1.0000000  0.0092966  0.0092966  4.1859740 0.0707  0.003
Residuals 55.0000000  0.1221491  0.0022209            0.9293
Total     56.0000000  0.1314457                       1.0000

This appears to be correct. Subsetting factors is something I always
struggle with in R, but, based on previous experience with lda(), it
seems that R generally does the right thing. Am I doing something wrong
here, or is there a problem in adonis?

Thanks,

Tyler

ps. Sorry for not supplying a reproducible bit of code. The data.frame
is quite large. The general layout is: