CCA分析:rowsum (X)中的错误:“X”必须是数字。

时间:2022-03-08 16:14:30

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

我试着从“素食”的包装上做一个CA分析。

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?

但是,我总是会得到一个错误:“rowsum (X)中的错误:‘X’必须是数字的”。我知道标签是一个因素,分析工作如果我删除第一列。但是随后的分析创建了自己的标签,我不能使用我的标签。是否有办法得到我自己的标签(plotA, plotB等)?

Many thanks in advance.

提前感谢。

.fidelfisch

.fidelfisch

1 个解决方案

#1


2  

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.

您需要将这些图变量存储为animalData数据框架的rowname属性,而不是作为实际数据中的变量。

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

然后对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)

CCA分析:rowsum (X)中的错误:“X”必须是数字。

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

正如您所看到的,已经使用了来自animalData数据框架的属性数据。

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.

另外,不要像这样附加()数据集;它不是必需的,实际上是危险的,因为数据不是附加的,而是独立的副本。

#1


2  

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.

您需要将这些图变量存储为animalData数据框架的rowname属性,而不是作为实际数据中的变量。

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

然后对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)

CCA分析:rowsum (X)中的错误:“X”必须是数字。

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

正如您所看到的,已经使用了来自animalData数据框架的属性数据。

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.

另外,不要像这样附加()数据集;它不是必需的,实际上是危险的,因为数据不是附加的,而是独立的副本。