将物种出现数据汇总为地块丰度数据

时间:2017-10-19 10:15:43

标签: r list dataframe aggregate summarize

我有一个包含情节,种类和事件的3列表格。我想总结数据,因为各个事件按图中的物种分组。我尝试了以下代码:

aggregate(occurrence ~ species, AbundTGLMSOn, FUN=sum)

但是这只按物种分组而不是地块内的物种,然后看起来像这样

   species occurrence
1   albleb          1
2   albodo         17
3   antgha          1
4   apovil         65
5   artlak          3
6   briret         18

非常感谢任何有关如何做到这一点的建议。

这是一个数据:

> dput(AbundTGLMSOn)
structure(list(plot = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L), .Label = c("H01", 
"H02", "H03", "H04", "H05", "L01", "L02", "L03", "L04", "L05", 
"O01", "O02", "O03", "O04", "O05"), class = "factor"), species = structure(c(30L, 
14L, 32L, 34L, 32L, 34L, 34L, 32L, 32L, 30L, 32L, 32L, 34L, 41L, 
30L, 30L, 32L, 34L, 34L, 29L, 30L, 30L, 30L, 32L, 32L, 30L, 30L, 
34L, 35L, 30L, 30L, 31L, 27L, 30L, 32L, 30L, 30L, 34L, 34L, 34L, 
32L, 41L, 41L, 41L, 41L, 32L, 32L, 32L, 41L, 40L, 41L, 10L, 41L, 
1L, 39L, 1L, 20L, 32L, 32L, 35L, 14L, 29L, 32L, 39L, 39L, 40L, 
40L, 1L, 40L, 40L, 41L, 22L, 40L, 32L, 32L, 37L, 10L, 10L, 32L, 
10L, 32L, 32L, 34L, 32L, 32L, 10L, 41L, 41L, 41L, 41L, 41L, 32L, 
32L, 40L, 32L, 32L, 10L, 35L, 7L, 32L, 32L, 32L, 32L, 3L, 32L, 
1L, 21L, 30L, 32L, 18L, 32L, 32L, 32L, 32L, 32L, 34L, 34L, 32L, 
34L, 34L, 30L, 30L, 32L, 32L, 32L, 32L, 34L, 24L, 30L, 34L, 18L, 
30L, 32L, 34L, 18L, 34L, 30L, 32L, 34L, 32L, 32L, 34L, 32L, 32L, 
14L, 32L, 32L, 32L, 32L, 34L, 32L, 32L, 32L, 41L, 41L, 32L, 32L, 
4L, 32L, 34L, 34L, 39L, 39L, 41L, 34L, 41L, 32L, 6L, 30L, 34L, 
34L, 39L, 30L, 34L, 30L, 34L, 34L, 30L, 30L, 32L, 34L, 30L, 34L, 
32L, 34L, 32L, 34L, 32L, 32L, 32L, 34L, 34L, 34L, 34L, 1L, 32L, 
32L, 32L, 34L, 34L, 32L, 32L, 34L, 34L, 18L, 32L, 34L, 34L, 24L, 
27L, 31L, 32L, 34L, 34L, 34L, 34L, 35L, 39L, 10L, 25L, 32L, 32L, 
34L, 18L, 30L, 32L, 34L, 34L, 34L, 34L, 34L, 34L, 32L, 30L, 34L, 
34L, 34L, 34L, 41L, 3L, 32L, 30L, 30L, 31L, 30L, 30L, 1L, 30L, 
30L, 32L, 32L, 32L, 32L, 32L, 34L, 1L, 32L, 34L, 32L, 1L, 24L, 
4L, 32L, 32L, 34L, 1L, 32L, 32L, 32L, 34L, 34L, 34L, 41L, 41L, 
34L, 13L, 17L, 32L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 41L, 
12L, 34L, 18L, 34L, 34L, 34L, 34L, 34L, 30L, 30L, 9L, 32L, 32L, 
14L, 1L, 14L, 32L, 32L, 32L, 32L, 1L, 26L, 32L, 32L, 11L, 13L, 
32L, 32L, 32L, 32L, 41L, 41L, 23L, 32L, 32L, 34L, 34L, 1L, 4L, 
32L, 41L, 4L, 13L, 32L, 34L, 24L, 41L, 32L, 33L, 30L, 32L, 34L, 
34L, 25L, 24L, 28L, 2L, 7L, 36L, 19L, 7L, 30L, 7L, 12L, 24L, 
30L, 6L, 24L, 30L, 12L, 35L, 7L, 35L, 14L, 28L, 14L, 33L, 28L, 
30L, 30L, 30L, 34L, 34L, 35L, 30L, 32L, 32L, 34L, 30L, 30L, 34L, 
34L, 34L, 32L, 32L, 32L, 34L, 28L, 32L, 32L, 32L, 32L, 32L, 32L, 
32L, 32L, 34L, 32L, 32L, 32L, 32L, 32L, 32L, 34L, 34L, 3L, 32L, 
32L, 34L, 34L, 34L, 16L, 32L, 32L, 32L, 34L, 34L, 34L, 32L, 32L, 
32L, 34L, 32L, 32L, 34L, 34L, 34L, 31L, 34L, 34L, 34L, 29L, 34L, 
34L, 34L, 34L, 34L, 41L, 8L, 34L, 3L, 30L, 34L, 34L, 34L, 34L, 
34L, 34L, 10L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 32L, 
32L, 32L, 24L, 32L, 34L, 32L, 32L, 32L, 32L, 11L, 32L, 32L, 32L, 
32L, 32L, 5L, 32L, 32L, 32L, 38L, 3L, 32L, 32L, 34L, 3L, 31L, 
32L, 34L, 34L, 6L, 13L, 31L, 32L, 32L, 32L, 32L, 32L, 34L, 34L, 
13L, 15L, 32L, 32L, 32L, 32L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 
32L, 34L, 34L, 34L, 34L, 34L, 1L, 32L, 7L, 11L, 32L, 32L, 32L, 
24L, 32L, 32L, 32L, 30L, 32L, 32L, 32L, 32L, 15L, 32L, 34L, 18L, 
32L, 32L, 32L, 34L, 7L, 7L, 34L, 32L, 32L, 34L, 7L, 35L, 7L, 
12L, 34L, 30L, 34L), .Label = c("apovil", "briret", "cansub", 
"carsph", "catspa", "dalcul", "dallan", "dalnig", "dilobo", "diptub", 
"elltom", "flesoo", "garcow", "garobt", "garsoo", "greeri", "halcor", 
"irvoli", "lancor", "lopdup", "mancal", "memscu", "milleu", "mitrot", 
"morcor", "ochint", "parama", "pavtom", "phyemb", "ptemac", "rotwit", 
"shoobt", "shorox", "shosia", "sinsia", "stegut", "strnux", "symrac", 
"syz001", "terche", "xylxyl"), class = "factor"), occurrence = c(1L, 
1L, 1L, 1L, 1L, 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L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("plot", "species", "occurrence"
), class = "data.frame", row.names = c(NA, -551L))

1 个答案:

答案 0 :(得分:0)

您只按species进行聚合,如果您想按图中的物种分组,则必须在公式的rhs中使用这两个变量。请注意,下面的两个命令给出相同的结果,唯一的区别是结果的显示方式。

sp1 <- aggregate(occurrence ~ species + plot, AbundTGLMSOn, FUN=sum)
head(sp1)
#  species plot occurrence
#1  garobt  H01          1
#2  parama  H01          1
#3  phyemb  H01          1
#4  ptemac  H01         14
#5  rotwit  H01          1
#6  shoobt  H01         10

sp2 <- aggregate(occurrence ~ plot + species, AbundTGLMSOn, FUN=sum)
head(sp2)
#  plot species occurrence
#1  H03  apovil          3
#2  L01  apovil          1
#3  L03  apovil          2
#4  L04  apovil          3
#5  L05  apovil          3
#6  O05  apovil          1