R中Excel表的行和列差异

时间:2019-04-17 18:55:48

标签: r

我正在尝试对安大略省北部的蜜蜂群落进行一些功能多样性分析。我正在使用R包SYNCSA,并尝试计算Rao的多样性。不断出现一个错误,说“来自社区数据的物种不在特征矩阵中。”

为了使用rao.diversity函数进行分析,我需要制作一个丰度数据表(例如2个蜜蜂群落以及其中每个物种有多少个)和特征表(例如群落及其花粉特异性,筑巢行为等,其中包含我已按数字排序的序数数据)。

现在,有一个使用此功能的规则,即群落(丰度)数据列中的物种名称必须与特征数据中各行的物种名称完全匹配。当我收到此错误时,据我所知,我立即确保社区数据的列与特征数据的行匹配,并且确实如此。我也尝试了函数organize.syncsa,该函数应该修复不匹配的列/行,只是为了获得错误消息“ comm必须仅包含数字或二进制变量”。我的通讯(即社区数据)确实只包含数字变量,所以我也不知道我在做什么错。

以下是我的两个表的ASCII文本表示形式,分别用于社区数据和特征数据:

dput(community.data.raos)
structure(list(X = structure(2:1, .Label = c("Akimiski", "Far North"
), class = "factor"), Andrena.algida = 1:0, Andrena.barbilabris = 1:0, 
    Andrena.frigida = 1:0, Anthophora.terminalis = 0:1, Bombus.flavidus = c(6L, 
    0L), Bombus.flavifrons = 2:1, Bombus.frigidus = c(65L, 27L
    ), Bombus.melanopygus = c(0L, 8L), Bombus.mixtus = 1:2, Bombus.sandersoni = c(21L, 
    0L), Bombus.sylvicola = c(4L, 17L), Bombus.ternarius = c(5L, 
    15L), Bombus.terricola = c(8L, 0L), Bombus.vagans = c(9L, 
    0L), Colletes.americanus = 0:1, Colletes.impunctatus = c(0L, 
    6L), Colletes.nigrifrons = c(1L, 5L), Halictus.ligatus = c(0L, 
    2L), Halictus.rubicundus = c(2L, 0L), Halictus.tripartitus = 1:0, 
    Halictus.virgatellus = c(2L, 12L), Lasioglossum.ephialtum = c(2L, 
    0L), Lasioglossum.laevissimum = 1:0, Lasioglossum.inconditum = c(5L, 
    0L), Lasioglossum.quebecense = 1:0, Coelioxys.sodalis = 2:1, 
    Hoplitis.albifrons = 1:0, Megachile.circumcincta = 0:1, Megachile.frigida = 2:3, 
    Megachile.gemula = 1:2, Megachile.lapponica = c(1L, 1L), 
    Megachile.melanophaea = c(0L, 3L), Megachile.montivaga = 1:0, 
    Megachile.perihirta = c(0L, 3L), Megachile.relativa = 1:2, 
    Osmia.bucephala = c(1L, 1L), Osmia.laticeps = 1:0, Osmia.nigrifrons = c(2L, 
    0L), Osmia.nigriventris = c(5L, 1L), Osmia.paradisica = c(0L, 
    8L), Osmia.tarsata = 1:0, Osmia.tersula = 1:0), class = "data.frame", row.names = c(NA, 
-2L))


dput(trait.table.raos)
structure(list(X = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 13L, 14L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 
24L, 26L, 25L, 27L, 15L, 23L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 
35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L), .Label = c("Andrena.algida ", 
"Andrena.barbilabris ", "Andrena.frigida ", "Anthophora.terminalis", 
"Bombus.flavidus", "Bombus.flavifrons ", "Bombus.frigidus ", 
"Bombus.melanopygus ", "Bombus.mixtus", "Bombus.sandersoni ", 
"Bombus.sylvicola", "Bombus.ternarius ", "Bombus.terricola ", 
"Bombus.vagans ", "Coelioxys.sodalis ", "Colletes.americanus", 
"Colletes.impunctatus", "Colletes.nigrifrons", "Halictus.ligatus", 
"Halictus.rubicundus", "Halictus.tripartitus", "Halictus.virgatellus ", 
"Hoplitis.albifrons ", "Lasioglossum.ephialtum", "Lasioglossum.inconditum", 
"Lasioglossum.laevissimum ", "Lasioglossum.quebecense ", "Megachile.circumcincta ", 
"Megachile.frigida", "Megachile.gemula", "Megachile.lapponica", 
"Megachile.melanophaea", "Megachile.montivaga ", "Megachile.perihirta", 
"Megachile.relativa", "Osmia.bucephala", "Osmia.laticeps", "Osmia.nigrifrons", 
"Osmia.nigriventris", "Osmia.paradisica ", "Osmia.tarsata", "Osmia.tersula "
), class = "factor"), Pollen.Specificity = c(2L, 2L, 1L, 2L, 
0L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 0L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 
1L, 2L, 2L, 2L, 1L, 2L), Tongue.Length = c(0L, 0L, 0L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L), Sociality = c(1L, 1L, 1L, 1L, 0L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 3L, 2L, 2L, 3L, 3L, 2L, 
1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), Nesting.Behavior = c(0L, 0L, 0L, 2L, 1L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 2L, 1L, 
0L, 1L, 1L), Average.Female.ITD = c(NA, NA, NA, NA, 3.64, NA, 
3.02, NA, 4.52, 3.21, 3.67, 4.17, 4.14, 3.59, NA, 2.15, 1.97, 
NA, 2.21, NA, 1.79, NA, NA, 1.43, NA, NA, NA, NA, 3.14, 3.57, 
3.21, 3.24, NA, 3.92, NA, 4, NA, 3.29, 3.86, 3.09, NA, NA)), class = "data.frame", row.names = c(NA, 
-42L))

还有我得到的错误示例:

rao.diversity(comm = community.data.raos, traits = trait.table.raos)
Error in rao.diversity(comm = community.data.raos, traits = trait.table.raos) : 

 There are species from community data that are not on traits matrix
> 

rao.diversity函数应该为每个站点(在我的例子中,有两个这样的站点)产生一系列小表,其值在0和1之间,以指示不同的多样性度量。取而代之的是,我收到以下错误消息:“社区数据中有某些物种不在特征矩阵中。”

1 个答案:

答案 0 :(得分:0)

文档并没有说得很清楚,但是看at the code of the function,我们可以看到社区对象的列名称必须与traits对象的行名称匹配。有了这些信息,我们可以查看您的输入并确定一些问题:

  • 您的社区对象的第一列X不是物种。摆脱掉它,如果您想保留这些信息,则将这些值转换为行名。

  • 您的traits对象在第一列中具有树种,但它们必须是行名。

  • 您的物种标签在两个对象之间不一致:在特征表中,其中一些具有尾随空白。使用trimws()摆脱它,使匹配正确。

如果仍然遇到问题,则可能需要查看setdiff(row.names(trait.table.raos), colnames(community.data.rao))和相反的setdiff(colnames(community.data.rao), row.names(trait.table.raos)),以确保物种名称之间的匹配正确。这两个语句均不应返回任何结果。