CCA分析:rowSums(X)中的错误:'x'必须是数字。

3

我尝试从 vegan 包中进行 CA 分析。

以下是我使用的代码:

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)

但是,我总是遇到一个错误:

Error in rowSums(X) : 'x'必须是数字

我知道标签是一个因子,如果我删除第一列,分析就会起作用。但是,然后分析创建自己的标签,我不能使用我的标签。有没有办法将我的标签(plotA、plotB等)包括进去?

请查看 data.frame 选项中的 row.names,通过 ?data.frame 查找。 - liuminzhao
1个回答

3
你需要将名为plots的变量存储为animalData数据框的rownames属性,而不是实际数据中的变量。
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
< p > animalData现在应该是这样的:

> 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

然后对于CA(证书颁发机构):
animalData.ca <- cca(animalData)

哪些是有效的:

> 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

绘制此对象会产生:
plot(animalData.ca, type = "text", scaling = 3)

enter image description here

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

此外,不要像这样attach()数据集;这是不必要的,而且实际上是危险的,因为数据没有被附加,而是独立的副本。


非常好。非常感谢!你救了我的一天/一周 :-) - fidelfisch

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接