CCA analysis: Error in rowSums(X) : 'x' must be numeric

fidelfisch picture fidelfisch · Nov 15, 2012 · Viewed 7.2k times · Source

I try to do a CA analysis from the vegan package.

This is the code I use:

install.packages("vegan")
library(vegan)

plots <- c("plotA", "plotB", "plotC", "plotD", "plotE")
animal1 <- c(2,7,4,8,1)
animal2 <- c(4,3,7,1,0)
animal3 <- c(8,5,0,1,3)
animal4 <- c(2,2,9,5,2)
animal5 <- c(1,6,9,8,7)

animalData <- data.frame (plots, animal1, animal2, animal3, animal4, animal5)
attach(animalData)

animalData.ca <- cca(animalData)

But then, I always get an error:

Error in rowSums(X) : 'x' must be numeric

I know that the labels are a factor and the analysis works if I delete the first column. But then the analysis creates own labels and I cannot use mine. Is there a way to get my own labels (plotA, plotB etc. ) included?

Answer

Gavin Simpson picture Gavin Simpson · Nov 15, 2012

You need to have the plots variable stored as the rownames attribute of the animalData data frame, not as a variable in the actual data.

You want:

library(vegan)

plots <- c("plotA", "plotB", "plotC", "plotD", "plotE")
animal1 <- c(2,7,4,8,1)
animal2 <- c(4,3,7,1,0)
animal3 <- c(8,5,0,1,3)
animal4 <- c(2,2,9,5,2)
animal5 <- c(1,6,9,8,7)

animalData <- data.frame(animal1, animal2, animal3, animal4, animal5)
rownames(animalData) <- plots

animalData now should look like this:

> animalData
      animal1 animal2 animal3 animal4 animal5
plotA       2       4       8       2       1
plotB       7       3       5       2       6
plotC       4       7       0       9       9
plotD       8       1       1       5       8
plotE       1       0       3       2       7

Then for the CA

animalData.ca <- cca(animalData)

which works:

> animalData.ca
Call: cca(X = animalData)

              Inertia Rank
Total          0.3793     
Unconstrained  0.3793    4
Inertia is mean squared contingency coefficient 

Eigenvalues for unconstrained axes:
     CA1      CA2      CA3      CA4 
0.219528 0.099206 0.055572 0.005018

Plotting this object results in

plot(animalData.ca, type = "text", scaling = 3)

enter image description here

which as you can see, has used the attribute data from the animalData data frame.

Also, don't attach() data sets like this; it isn't required and is in fact dangerous as the data are not attached, but an independent copy.