gpt4 book ai didi

How to solve the error "Species labels in 'x' and 'a' need to be identical and ordered alphabetically (or simply in the same order)" in R?(如何解决R中的“x‘和’a‘中的物种标签需要相同并按字母顺序(或简单地按相同顺序)排序”的错误?)

转载 作者:bug小助手 更新时间:2023-10-25 11:49:49 46 4
gpt4 key购买 nike



I want to compute the CWM (community weighted mean) with the function "functcomp", but I always have the same error which is "Species labels in 'x' and 'a' need to be identical and ordered alphabetically (or simply in the same order)".

我想用函数“unctcomp”计算CWM(社区加权平均值),但我总是有相同的错误,那就是“‘x’和‘a’中的物种标签需要相同,并按字母顺序(或简单地按相同的顺序)排序”。


Here is my code:

以下是我的代码:


TablePlot <- table(data_pre$NamePlot,data_pre$Genius_Species)
MatrixSpPlot <- as.matrix(TablePlot)
DFSpEV <- data.frame(data_pre[,c(9,11,12,13,14,15)])
DFSpEV <- aggregate(x = DFSpEV[,c(2,3,4,5,6)], by = list(DFSpEV$Genius_Species), FUN = mean, na.rm = T)
colnames(DFSpEV) <- c("Genius_Species","Soil_humidity","Soil_pH","Soil_nutrients","Light","Temperature")
functcomp(DFSpEV,MatrixSpPlot,CWM.type = c("dom"), bin.num = NULL)

In this code, I created a species presence/absence matrix for each site (MatrixSpPlot), and then I created a dataframe DFSpEV containing all species and their ecological indicator values (temperature, light, pH, soil nutrients and soil humidity), and I aggregated repeating species.

在这段代码中,我为每个站点(MatrixSpPlot)创建了一个物种存在/缺失矩阵,然后创建了一个包含所有物种及其生态指标值(温度、光照、pH、土壤养分和土壤湿度)的数据帧DFSpEV,并聚合了重复的物种。


I don't understand this error, I tried to verify my data, the species are in the same order and are identical between my matrix and my data frame. What I have doubts about is the way I've created my matrix.

我不明白这个错误,我试图验证我的数据,物种是在相同的顺序,是我的矩阵和我的数据框架相同。我怀疑的是我创建矩阵的方式。


Can you help me please? Thank you so much!
Eléa

你能帮帮我吗?非常感谢!埃莱亚


更多回答

My answer is a formalization of steps I'd recommend, even though you say you checked them. The final note about spelling and case-sens is important, you might not see it easily but the expressions in the answer should help figure out which one(s) is/are different. If this still does not help resolve it for you, I suggest you make your question more reproducible by adding sample data, see: stackoverflow.com/q/5963269 , minimal reproducible example, and stackoverflow.com/tags/r/info for discussions on using dput to add unambiguous sample data to your question. Thanks, and welcome to SO!

我的答案是将我推荐的步骤正规化,即使你说你检查了它们。关于拼写和大小写的最后一点很重要,你可能不容易看懂,但答案中的表达应该有助于找出哪个(S)是不同的。如果这仍然不能帮助您解决问题,我建议您通过添加样本数据来使您的问题更具重复性,有关使用dput向问题添加明确的样本数据的讨论,请参阅:stackoverflow.com/q/5963269,Minimum Reducable Example,和stackoverflow.com/tag/r/info。谢谢,欢迎来到So!

优秀答案推荐


Looking at the code for that function, specifically at

查看该函数的代码,特别是


s.x <- dim(x)[1]
s.a <- dim(a)[2]
if (s.x != s.a) stop("Different number of species in 'x' and 'a'.","\n")
if (any(x.n != a.n) ) stop("Species labels in 'x' and 'a' need to be identical and ordered alphabetically (or simply in the same order).","\n")

my guess is that your row names of x and column names of a must not only contain all the same names, but also be in the same order.

我的猜测是,x的行名和a的列名不仅必须包含所有相同的名称,而且顺序也必须相同。


Some possible checks to resolve your issue, where names in one that are not in the other will either need to be removed from the one or added (if data can be found) in the other.

一些可能的检查以解决您的问题,其中一个中不在另一个中的名称将需要从一个中删除或在另一个中添加(如果可以找到数据)。


rownames(DFSpEV) # should be not-empty
colnames(a) # same

# this shows rows in `a` that are not present in `x`
setdiff(rownames(DFSpEV), colnames(MatrixSpPlot))
# ditto, in reverse
setdiff(colnames(MatrixSpPlot), rownames(DFSpEV))

If the two setdiffs are empty, then you have them all, and this next command is likely failing in at least two locations:

如果这两个setDiffs为空,那么您拥有它们的全部,并且下一个命令可能在至少两个位置失败:


rownames(DFSpEV) == colnames(MatrixSpPlot)

One way that should resolve this is to make sure we use only those rownames/column-names that are present in both, and use the same order for both arguments:

解决这个问题的一种方法是确保我们只使用出现在两者中的行名/列名,并对两个参数使用相同的顺序:


nms <- intersect(rownames(DFSpEV), colnames(MatrixSpPlot))
functcomp(DFSpEV[nms,], MatrixSpPlot[,nms], CWM.type = c("dom"), bin.num = NULL)

Using nms here has the effect of removing species that are in one and not the other, and ensuring all remaining species are in the same order.

在这里使用NMS的效果是移除一个而不是另一个中的物种,并确保所有剩余的物种都是相同的顺序。


Another thing to consider: spelling and capitalization. The strict test of != does not tolerate change in order, spelling, upper/lower-case, and even (I have no idea about your data) locale/unicode/UTF differences.

另一件需要考虑的事情是:拼写和大写。!=的严格测试不允许更改顺序、拼写、大小写,甚至(我不了解您的数据)地区/Unicode/UTF差异。


更多回答

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com