CCA analysis: Error in rowSums(X) : 'x' must be numeric
Matthew Harrington
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?
11 Answer
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) <- plotsanimalData 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 7Then 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.005018Plotting this object results in
plot(animalData.ca, type = "text", scaling = 3)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.