我有一个BTO数据集,我将其从长格式转换为宽格式,以便使用vegan
包中的diversity
函数为多样性测量做好准备。
为了实现这一点,我使用了以下代码:
diversity <- pivot_wider(bird_case, names_from = ENGLISH_NAME, values_from = HOW_MANY)
当我用以前的代码将月份转换为季节时,结果会出现列表元素。我想取所有列表的和,所以每个单元格中只剩下一个和。至于NULL值,我希望这些值转换为0。
我已经尝试使用这个将NULL值替换为零
diversity[diversity == "NULL"] <- 0
它不会起作用。
至于转换列表元素并取和,我尝试过聚合,但没有成功。
这是一个可复制的代码:
structure(list(year = c(2018, 2019, 2017, 2015, 2014, 2015, 2017,
2017, 2016, 2019, 2018, 2016, 2016, 2016, 2019, 2019, 2018, 2017,
2015, 2018, 2015, 2017, 2015, 2016, 2016, 2016, 2018, 2018, 2017,
2014, 2015, 2017, 2014, 2014, 2017, 2019, 2010, 2011, 2011, 2012,
2019, 2012, 2013, 2019, 2017, 2011, 2017, 2016, 2016, 2010),
Season = c("Winter", "Winter", "Summer", "Winter", "Winter",
"Autumn", "Autumn", "Winter", "Spring", "Autumn", "Spring",
"Winter", "Summer", "Autumn", "Summer", "Spring", "Summer",
"Spring", "Spring", "Autumn", "Summer", "Summer", "Autumn",
"Summer", "Autumn", "Winter", "Spring", "Winter", "Winter",
"Summer", "Winter", "Autumn", "Autumn", "Winter", "Spring",
"Winter", "Summer", "Spring", "Summer", "Autumn", "Winter",
"Winter", "Winter", "Spring", "Summer", "Winter", "Autumn",
"Winter", "Spring", "Winter"), POSTCODE = c("NR29 5QA", "NR29 5QA",
"NR29 5QA", "NR29 5QA", "NR29 5QA", "NR29 5QA", "NR29 5QA",
"NR29 5QA", "NR29 5QA", "NR29 5QA", "NR29 5QA", "NR29 5QA",
"NR29 5QA", "NR29 5QA", "NR29 5QA", "NR29 5QA", "NR29 5QA",
"NR29 5QA", "NR29 5QA", "NR29 5QA", "NR29 5QA", "NR15 1TS",
"NR15 1TS", "NR15 1TS", "NR15 1TS", "NR15 1TS", "NR15 1TS",
"NR15 1TS", "NR15 1TS", "NR15 1TS", "NR15 1TS", "NR15 1TS",
"NR15 1TS", "NR15 1TS", "NR15 1TS", "NR15 1TS", "PE32 1TL",
"PE32 1TL", "PE32 1TL", "PE32 1TL", "PE32 1TL", "PE32 1TL",
"PE32 1TL", "PE32 1TL", "PE32 1TL", "PE32 1TL", "PE32 1TL",
"PE32 1TL", "PE32 1TL", "PE32 1TL"), LOC_ID = c("LOC568364",
"LOC568364", "LOC568364", "LOC568364", "LOC568364", "LOC568364",
"LOC568364", "LOC568364", "LOC568364", "LOC568364", "LOC568364",
"LOC568364", "LOC568364", "LOC568364", "LOC568364", "LOC568364",
"LOC568364", "LOC568364", "LOC568364", "LOC568364", "LOC568364",
"LOC1163128", "LOC1163128", "LOC1163128", "LOC1163128", "LOC1163128",
"LOC1163128", "LOC1163128", "LOC1163128", "LOC1163128", "LOC1163128",
"LOC1163128", "LOC1163128", "LOC1163128", "LOC1163128", "LOC1163128",
"LOC569508", "LOC569508", "LOC569508", "LOC569508", "LOC569508",
"LOC569508", "LOC569508", "LOC569508", "LOC569508", "LOC569508",
"LOC569508", "LOC569508", "LOC569508", "LOC569508"), Wren = list(
c(1, 1, 1, 1, 1, 1, 1, 1), c(1, 1, 1, 1, 1, 1, 1, 1,
1, 1), c(1, 1, 1, 1), c(1, 1, 1, 1, 1, 1, 1), 1, c(1,
1, 1, 1, 1), c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), c(1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1), c(1, 1, 1, 1, 1, 1), c(1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1), c(1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1), c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), c(1, 1, 1,
1, 1, 1, 1, 1, 1, 1), c(1, 1, 1, 1, 1, 1), c(1, 1, 1,
1, 3, 1, 1, 1), c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), c(1,
1, 1, 1, 1, 1, 1, 1), c(1, 1, 1, 1, 1, 1, 2, 1, 1), c(1,
1, 1, 1, 1, 1, 1), c(1, 1, 1, 1, 2, 1), c(2, 1, 2, 3,
1, 1, 1), c(1, 1), c(1, 1, 1), c(1, 1, 1, 1, 1), c(1,
1), c(1, 1), c(1, 1, 1), c(1, 1, 1, 1, 1, 1, 1, 1, 1),
NULL, 1, c(2, 1, 1, 2, 1), NULL, NULL, c(1, 1, 1), NULL,
1, NULL, NULL, c(1, 1), c(1, 1, 1, 1, 1, 1), c(1, 1,
1, 1), c(1, 1), NULL, NULL, c(1, 1), 1, c(1, 1, 1), NULL,
c(1, 1, 1, 1)), Dunnock = list(c(2, 2, 1, 2, 1, 1, 1,
2, 2, 2), c(2, 1, 2, 2, 2, 2, 1), c(1, 1, 2, 1, 3, 1, 2),
c(1, 2, 2, 2, 2, 2, 2, 1, 1), 2, c(1, 1, 1, 2, 1, 1,
2), c(1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1), c(2, 2, 2, 2,
2, 1, 2, 2, 1, 2, 2), c(2, 5, 2, 1, 3, 2, 2, 3, 2, 3,
1), c(2, 1, 1, 2, 2), c(3, 2, 3, 2, 2, 3, 3, 2, 2), c(1,
1, 1, 1, 1, 1), c(1, 2, 1, 2, 2, 2, 2), c(1, 1, 2, 1,
2, 1, 2, 2, 1, 2), c(3, 4, 2, 5, 3, 5, 4, 2), c(2, 2,
2, 2, 1, 2, 3, 2, 2), c(3, 3, 3, 3, 3, 2, 1, 2, 3, 1),
c(2, 3, 2, 2, 2, 2, 2, 2, 2, 5, 4, 2), c(2, 2, 2, 1,
2, 1, 2, 2, 2, 2), c(1, 2, 1, 1, 2, 2, 1), c(1, 1, 1,
2, 1, 1), c(3, 4, 6, 3, 3, 3), c(1, 1, 2, 1), c(2, 1),
c(2, 2, 1, 2, 1), c(2, 1, 2, 1, 2, 2), c(2, 2), c(1,
1, 1, 2), c(2, 3, 2, 2, 2, 3, 3, 2, 2, 2), 2, c(2, 2,
3, 2, 2), c(2, 1, 2, 2, 2, 2), 1, NULL, c(3, 2), c(1,
1), c(1, 2, 1, 1, 1, 1, 1, 2), c(2, 2, 1, 1, 1, 1), c(3,
3, 2, 1, 2, 2, 2, 1, 1, 1), c(2, 1, 1, 1, 1), c(1, 1,
1, 1, 1, 2, 2, 2), c(1, 1, 2, 1, 1, 2, 1, 1), c(1, 1,
2, 1, 1), c(3, 2, 1, 5, 2, 1, 2, 2, 2), c(3, 3, 1, 1,
1, 3, 2, 1), c(1, 1, 2, 1), c(1, 2, 2, 2, 1, 2, 1, 2,
2, 2, 1, 1), c(1, 1, 2, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1),
c(1, 1, 2, 1, 4, 4, 1, 2, 2, 2), c(2, 1, 1, 4, 1, 1,
1, 2, 1, 1)), `Blue Tit` = list(c(1, 1, 2, 3), c(2, 2,
3, 2, 2), 4, c(4, 2, 3, 4), 2, c(2, 2), c(1, 2, 2), c(2,
2), c(2, 2, 1, 2, 2), NULL, c(2, 2, 2, 5, 2, 2, 2, 2, 2),
c(2, 1, 2, 2, 3, 2), 2, NULL, 7, c(2, 2, 2, 2, 2, 2,
2, 2, 2), NULL, c(1, 1, 2, 2, 2, 2, 2), c(4, 2, 4, 3,
7, 3, 2), 1, c(2, 2, 3), c(8, 10, 10, 12, 10, 8, 5, 12
), c(6, 4, 4, 6, 4), c(12, 6, 6, 6, 6), c(4, 4, 5, 5,
8), c(10, 6, 6, 4, 6, 6, 4), 4, c(10, 4, 4, 8, 6), c(4,
6, 4, 10, 6, 6, 8, 7, 6), c(12, 12, 6), c(12, 8, 12,
12, 12, 10, 10), c(10, 5, 10, 5, 10), c(12, 12, 6), c(6,
6), c(4, 2, 2, 2), c(2, 6), c(3, 2, 2, 1, 2, 1, 2), c(2,
2, 2, 1, 2, 1), c(2, 4, 1, 2, 1, 2, 2, 1, 2), c(4, 3,
1, 2, 2, 2, 2, 3, 5, 4), c(2, 4, 3, 3, 1, 2, 2), c(2,
4, 2, 2, 1, 2, 1, 1, 3), c(3, 3, 2, 2, 3, 2, 3, 2), c(1,
2, 1, 2, 2, 2, 2, 1, 2), c(5, 3, 9, 4, 4, 3, 9, 5), c(1,
2, 1, 2, 3, 2, 1, 2, 3, 3, 2), c(4, 3, 5, 2, 3, 4, 3,
3, 4, 5, 2), c(3, 3, 3, 3, 4, 2, 3, 4, 3, 5, 3), c(2,
2, 2, 1, 1, 2, 1, 2, 1, 2, 4), c(2, 2, 2, 3, 2, 2, 2,
1)), `Pied/White Wagtail` = list(c(1, 2, 2, 2, 1, 1,
1, 2, 2, 2), c(2, 1, 1, 1, 2, 1, 1, 2, 2), c(1, 1, 1, 1),
NULL, NULL, NULL, 2, c(2, 2, 2, 2), c(1, 1, 1, 1, 1),
c(2, 2, 2, 1), c(2, 2, 2, 2, 2, 2, 2, 2, 2), NULL, c(2,
2), NULL, c(2, 2, 1, 2, 2, 2), c(2, 2, 2, 2, 2, 2, 1,
2, 2, 2), c(2, 2, 2, 2, 1, 2, 3), c(1, 2, 2, 2, 2, 2),
NULL, c(1, 1), 1, 1, NULL, NULL, 1, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1, NULL, 1, c(1, 1, 1, 1, 1), c(2, 1, 2, 1), c(1, 1),
c(1, 1), 1, 1, 1, 1, c(1, 1), c(1, 1, 1, 1)), `Collared Dove` = list(
c(2, 2, 2, 2, 2, 2, 2, 2, 2), c(2, 2, 3, 2, 2, 2, 2,
3, 2, 2), c(2, 3, 2, 2, 2, 2, 2, 3, 3), c(1, 1, 2, 2),
NULL, c(2, 2, 2, 2, 2), c(2, 2, 2, 2, 2, 2, 2, 2, 2,
3), c(2, 2, 2, 1, 2, 2, 2, 1, 1), c(2, 2, 2, 2, 2, 2,
2), c(2, 2, 4, 4, 2, 2, 22, 2), c(2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2), c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
c(2, 2, 2, 2, 2, 2, 2, 2, 2), c(2, 2, 2, 2, 2, 2, 2,
2), c(2, 3, 3, 4, 2, 2, 2, 2, 2), c(2, 3, 3, 3, 2, 3,
3, 3, 3, 3, 2), c(2, 1, 2, 2, 2, 3, 2, 2, 2, 2, 2, 3),
c(2, 2, 2, 2, 2, 2), c(1, 2, 2, 1, 2), c(2, 2, 2, 2,
2, 2, 2, 2, 2), c(1, 2, 2, 1, 2), c(2, 2, 2, 2, 2, 2,
1), c(1, 1), c(1, 1), c(1, 2, 2, 2, 1), c(2, 2, 1, 2),
2, c(2, 1), c(3, 1, 1, 1, 1, 2, 2), NULL, c(2, 1, 1),
c(2, 2), 1, 1, c(2, 2), NULL, c(9, 9, 17, 8, 19), c(6,
3, 2, 3, 3, 5, 3), c(16, 9, 12, 3, 7, 5), c(4, 4, 3,
3, 5, 3, 2), c(2, 2, 3, 3, 2, 3, 4, 2), c(2, 2, 2, 3,
4, 4, 2, 12, 3, 5, 4), c(2, 2, 3, 3, 2, 3, 2, 3, 3),
c(3, 3, 3, 3, 2, 5, 3, 1, 3), c(4, 2, 3, 2, 7, 2, 3),
c(3, 1, 12, 3, 4, 4, 2, 5, 5, 12), c(3, 2, 1, 5, 3, 2,
2, 1, 2, 3, 2), c(3, 2, 2, 5, 3, 3, 2, 2, 10), c(2, 2,
1, 1, 3, 2, 1, 1, 2), c(6, 2, 6, 2, 5, 3, 2, 2, 4, 11,
3, 2)), `Great Tit` = list(c(1, 2, 1, 1, 1, 1, 1, 1),
c(1, 2, 1, 2, 1, 2, 1, 1), NULL, c(1, 3, 2, 5, 3, 3,
4, 1), NULL, c(1, 2, 1, 1), c(1, 1), NULL, c(1, 1, 1,
2, 1, 1), 1, c(1, 1), c(1, 1, 1, 1), 1, NULL, c(2, 2,
1, 1), c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2), 1, 3, c(2, 2,
2, 1), c(4, 2, 1, 2), c(2, 1), c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, 1, 1, 1, 2,
2, 2, 2, 1), c(1, 1, 1, 1, 2, 1, 1), c(1, 3, 3, 3), c(3,
1, 1, 2), c(1, 1, 1, 1, 1), c(2, 2, 2, 2, 2, 3, 2), c(2,
1, 2, 1, 3), c(2, 2), c(3, 1, 3, 5, 2, 2, 2, 2, 2), c(3,
4, 4, 2, 3, 2, 2, 4, 2), 1, c(4, 2, 4, 2, 4), c(1, 1, 3),
c(2, 2, 2), 2, c(3, 2, 2), c(1, 2), c(1, 1, 1, 1, 1),
c(3, 2, 2, 2, 4, 2, 2, 1), c(2, 1, 2, 1, 1), c(1, 1,
1, 1), c(1, 1, 1, 1, 1, 1, 1, 1, 1), c(1, 1, 2, 1, 1),
c(1, 1, 1, 1, 1), c(1, 1, 1, 1, 1, 1, 1), c(2, 4, 3,
2, 1, 6, 2, 3, 1, 2), c(1, 1, 2, 1, 1, 1, 2, 1, 2, 1),
c(1, 1, 1, 1, 1, 2, 1, 1, 1), c(1, 1, 1, 2, 1, 1, 1,
1, 1), c(2, 1, 2, 1, 2, 1, 2, 1), c(1, 1, 1, 1, 2, 1,
1, 1, 2)), Greenfinch = list(2, c(2, 2, 2, 2, 2, 2, 2
), 1, c(1, 1, 2), NULL, NULL, NULL, c(2, 2, 2), c(3, 1, 2,
3, 3), 2, c(2, 5, 2, 2, 2, 2, 2, 5, 2, 2), NULL, c(2, 1,
3, 2), NULL, c(1, 2, 1, 2, 1, 1), c(2, 2), 2, 1, c(2, 2,
2, 1, 2, 2, 1, 2, 2, 2), NULL, c(3, 1, 3), c(4, 2, 4), 1,
c(2, 2, 4, 3, 2, 2), c(2, 2, 1, 2, 4, 2, 2, 2), c(2,
2, 3, 2, 3), c(3, 1), c(2, 2, 2, 2, 3), c(2, 6, 4, 2,
2, 2), 4, c(5, 5, 5, 5), c(2, 2, 1, 4, 2, 4, 4), 4, c(2,
2), c(4, 1, 4), 2, c(7, 2, 3, 2, 2, 3, 4, 4, 3), c(4,
3, 2, 1, 2, 2, 2), c(6, 1, 3, 2, 1, 2, 2), c(3, 1, 2,
3), 1, c(1, 1, 3, 3, 1, 5, 2, 1, 1, 3, 1), c(1, 2, 2,
2, 3, 1, 3), c(1, 1, 3, 1, 1, 3, 1), c(1, 4, 1, 3, 4),
c(2, 2, 1, 1, 1), c(2, 2, 5, 2, 1, 2, 1, 1), c(7, 2,
6, 1, 2), c(2, 1, 2, 1, 1), c(4, 2, 1, 1, 2, 1)), `House Sparrow` = list(
NULL, c(2, 2, 2, 2, 2, 2), NULL, c(2, 2, 4, 6, 3, 4,
3, 3), 3, c(3, 2, 2, 2), NULL, NULL, NULL, c(2, 2, 2),
c(1, 2, 2), c(2, 2), NULL, NULL, c(3, 5), c(2, 2, 2,
2, 2, 2, 2, 2), NULL, NULL, c(3, 3, 3, 3, 2, 3, 5, 3,
3, 3), NULL, c(2, 2, 1, 1, 1, 2), NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, c(20, 14, 12, 10, 8, 14, 21), c(6, 5, 9,
9, 9, 6), c(13, 12, 5, 21, 11, 12, 16, 10, 15), c(3,
2, 7, 3, 1), c(10, 11, 15, 8, 12, 15, 5, 16), c(1, 5,
5, 5, 5, 4, 5, 2, 6, 4, 4), c(2, 4, 1, 4, 3, 3, 7, 7,
3, 5, 3), c(9, 10, 10, 7, 8, 10, 10, 6, 8, 6, 12, 9),
c(10, 5, 13, 14, 4, 5, 9, 9, 10, 8, 9), c(10, 9, 10,
7, 9, 10, 8, 7, 9, 14), c(3, 7, 5, 10, 2, 6, 14, 6, 3,
7, 3), c(7, 9, 11, 5, 5, 7, 7, 6, 6, 10, 5, 7, 16), c(5,
7, 5, 5, 6, 8, 7, 4, 5), c(15, 10, 12, 9, 3, 9, 10, 11
)), `Coal Tit` = list(1, c(1, 1, 1), NULL, c(2, 3, 2,
2, 4, 2, 2), NULL, 2, NULL, NULL, c(1, 1, 1), NULL, c(2,
1), 1, 1, NULL, 2, NULL, 1, NULL, 2, 1, 2, c(1, 1, 1), c(2,
2, 2, 2, 2, 1), c(1, 1, 1, 1, 1), c(1, 1, 1, 1), c(1, 1,
2), 1, c(2, 1, 1, 1, 1, 1), c(2, 2, 1, 2, 1, 2, 2), c(2,
2), c(2, 2, 2), c(2, 2, 2, 2, 2, 2, 2, 2), c(2, 2, 2), 2,
c(1, 1), NULL, 1, NULL, c(1, 1, 1), c(2, 1, 1, 2, 2,
1, 1, 3, 1), c(1, 1, 1, 1), c(2, 1, 1, 1, 1, 2), c(1,
1, 1, 2, 1, 1), NULL, c(2, 1, 1, 1), c(1, 1), c(1, 2,
1, 1, 2, 1, 2, 1, 2), c(1, 2, 1, 2, 1, 1, 1), c(1, 1,
1, 1, 1, 1, 1, 1, 1), 1), Woodpigeon = list(c(2, 3, 3,
3, 3, 3, 3, 5, 3), c(3, 4, 3, 3, 3, 5, 3, 3, 2, 4, 3), c(2,
1, 3, 3, 3, 3, 1, 3), c(3, 3, 3, 4, 1, 1, 5, 5, 5), 2, c(3,
4, 1, 3, 3, 3, 1, 5, 3), c(2, 6, 5, 3, 7, 5, 2, 1, 3, 2,
2), c(3, 3, 3, 3, 2, 2, 3, 4, 3, 3, 5, 5, 1), c(5, 5, 5,
3, 5, 4, 4, 5, 7), c(5, 4, 3, 4, 5, 4), c(3, 3, 3, 3, 3,
3, 5), c(3, 2, 3, 3, 3, 5, 6, 3, 3, 5, 5), c(5, 3, 2, 5,
3, 5, 3, 3, 3), c(2, 3, 3, 3, 4, 5, 3, 5, 5), c(3, 3, 3,
3, 5, 3, 3, 2, 3), c(3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 2), c(3,
2, 3, 3, 3, 3, 3, 3, 3, 3, 3), c(3, 4, 3, 3, 5, 3, 3, 3),
c(3, 3, 4, 5, 5, 3, 3, 3), c(3, 3, 3, 3, 2, 3, 3, 3),
c(2, 2, 3, 3, 3, 2, 3, 3), c(4, 4, 4, 4), c(10, 8, 10
), c(5, 5, 5), c(6, 4, 6), c(6, 6, 6, 10, 6), c(6, 10
), c(20, 10, 10, 10, 4, 10, 8), c(6, 6, 4, 4, 6, 4, 4,
6, 6), NULL, c(8, 8, 8), c(3, 4, 4, 6, 3, 6, 3), NULL,
NULL, c(6, 4, 6, 4), 1, c(3, 3, 2, 3, 1, 3, 2, 1), c(5,
3, 4, 4, 2, 3, 3), c(1, 2, 5, 1, 4, 4, 3, 4, 2, 5, 2),
c(3, 2, 2, 1, 3, 2, 2, 1), c(5, 6, 2, 6, 2), c(1, 6,
2, 6, 2, 3, 3, 3), c(5, 3, 5, 4, 4, 2, 2, 5), c(5, 5,
3, 4, 2, 3, 5, 4), c(3, 2, 2, 2, 3, 2, 5), c(2, 2, 5,
3, 3, 5, 3, 4), c(2, 2, 1, 1, 5, 6, 2, 7, 5, 2, 3), c(5,
2, 3, 5, 2, 1, 5, 6, 4, 2), c(2, 3, 4, 3, 3, 4, 3, 3,
3, 3), c(7, 5, 3, 2, 5, 9, 2, 3, 3, 4, 3)), Blackbird = list(
c(3, 3, 1, 3, 3, 3, 3, 5, 5), c(3, 3, 3, 3, 3, 3, 5,
3, 3, 3), c(2, 1, 3, 3, 3, 3, 3), c(5, 5, 11, 7, 3, 11,
15, 10, 5, 3), NULL, c(7, 2, 9, 3, 6, 3, 2, 3, 5), c(5,
2, 3, 1, 3, 5, 2, 1), c(3, 3, 4, 1, 2, 3, 3, 2, 3, 4,
2), c(4, 3, 3, 5, 4, 5, 5, 4, 3, 3, 5, 3), c(11, 7, 5,
4, 11, 11, 5), c(2, 4, 2, 3, 5, 6, 3, 3), c(3, 3, 3,
3, 4, 4, 3, 3), c(3, 3, 2, 2, 2, 3, 4, 3, 2), c(5, 13,
3, 5, 7, 4, 3, 7), c(5, 8, 6, 5, 5, 6, 3, 5, 10), c(4,
3, 8, 4, 3, 6, 3), c(5, 5, 5, 2, 5, 3, 3, 3), c(3, 3,
3, 3, 5, 5, 4, 4, 5, 4, 3), c(3, 3, 4, 5, 4, 5, 5, 2),
c(5, 5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(1, 1, 1, 1, 1, 1, 1), c(1, 1, 1, 1,
1, 1), c(2, 1, 1, 1, 1, 1), c(2, 2, 1, 1), NULL, 1, 1, c(1,
1, 1), 1, c(1, 1, 2, 1), c(1, 2, 1, 2, 3, 1), NULL, c(1,
1), NULL, NULL, NULL, c(1, 2, 2), NULL, 1, c(1, 1), NULL,
NULL, NULL, NULL, NULL, NULL, 1, c(1, 1), 1, 1, c(1,
1, 2, 1, 2, 1, 1, 1, 1), NULL), Chaffinch = list(c(2,
1, 3, 3), c(2, 2, 2, 1, 2, 1), c(1, 2), c(1, 1, 3, 2, 2,
2, 2, 2, 1, 2), NULL, c(3, 3, 3, 2, 2, 2, 1), c(3, 1, 3,
2), NULL, c(5, 2, 2, 2, 4), c(2, 2), c(2, 2, 2, 2, 2, 3,
2, 2, 2, 2), c(5, 1, 3, 2, 3), c(2, 1, 1, 3), c(3, 2, 2,
2, 2, 3, 1), c(2, 2), c(2, 2, 2, 2, 2, 2, 2, 2, 2), c(2,
2, 3), c(2, 1, 2, 2, 2), c(4, 3, 2, 5, 2, 2), c(1, 3), c(3,
3, 1, 5, 1), c(2, 4, 5, 2, 2), c(2, 4, 2, 4, 1), c(6, 4,
4), c(5, 2, 4, 4, 5, 4, 4), c(6, 3, 4, 5, 4, 4, 3), c(4,
4, 4), c(4, 2, 6, 2), c(7, 6, 8, 8, 8, 4, 6, 4, 4, 4), c(10,
6), c(2, 6, 6, 4), c(4, 4, 5, 4, 4, 4, 5), c(10, 10, 10),
NULL, c(2, 2, 2, 4, 4, 4), NULL, c(6, 6, 5, 7, 3, 2),
c(4, 4, 2, 3, 10), c(1, 5, 3, 5, 4, 5, 3, 2, 4, 2), c(5,
4, 4, 2, 7, 6, 10, 2, 7, 2), c(2, 4, 2, 3, 4, 1, 4, 3,
1, 1), c(13, 7, 3, 6, 13, 9, 5, 7, 7, 11), c(10, 7, 9,
7, 9, 17, 11, 8, 4), c(1, 3, 3), c(1, 3, 4, 1, 1, 1,
2, 6, 4), c(5, 8, 6, 9, 9, 3, 11, 2, 5), c(2, 3, 3, 3,
2, 3), c(4, 3, 3, 5, 3, 4, 4, 4, 6, 3, 3, 3), c(3, 2,
3, 2, 3, 2, 4, 3, 2, 1, 2, 5, 3), c(12, 5, 12, 8, 18,
6, 3, 4, 9, 15, 7, 10)), Starling = list(c(1, 3), 1,
3, c(5, 5, 5, 5, 7, 7, 5), NULL, NULL, NULL, NULL, c(5,
9, 7, 5, 7, 7), NULL, c(1, 1, 2, 2, 1, 2), 3, NULL, NULL,
1, c(1, 3), c(1, 1), c(2, 2, 2, 2), c(5, 2, 1, 3, 7,
13, 1, 2, 2, 3), c(1, 2), NULL, NULL, NULL, NULL, NULL,
NULL, c(1, 1), NULL, 4, NULL, NULL, NULL, NULL, NULL,
c(4, 12), NULL, c(2, 28, 9, 2, 3, 9), c(3, 7, 8, 2, 3,
12, 3), c(2, 1, 6, 9, 18), c(11, 1, 5, 30, 10), c(25,
9, 8, 39, 20, 18, 30), c(15, 10, 9, 27, 14, 15, 30, 30,
19, 12), c(3, 8, 14, 2, 21, 19, 35), c(13, 8, 9, 21,
9, 28, 1, 5, 16), c(1, 2, 2, 1, 1, 8, 1), c(6, 27, 6,
25, 16, 10, 3, 40, 5, 30), c(2, 1, 3, 2, 3, 2, 1), c(6,
4, 24, 6, 8, 7, 9, 10), c(17, 3, 1, 11, 5, 5, 2, 6, 6,
5, 2, 3), c(2, 4, 1, 5, 3, 3, 14, 7, 5, 2, 6)), Goldfinch = list(
c(1, 3, 5, 1, 1), NULL, 2, NULL, NULL, NULL, c(2, 2,
2), c(3, 3, 3), c(2, 1), NULL, c(2, 3, 3, 2, 2, 3, 2),
NULL, NULL, NULL, NULL, NULL, NULL, NULL, c(2, 2, 2),
c(5, 3), 2, c(6, 10, 6, 6, 6, 8, 4), c(2, 6, 3), c(6,
4, 2), c(10, 10, 8, 8, 10, 10), c(1, 6, 6, 6, 1, 2, 6
), c(2, 2), c(2, 2, 4, 6), c(1, 4, 4, 4, 4, 8, 4, 6),
c(10, 8, 8), c(6, 6, 6, 2, 1), c(7, 8, 5, 8, 4), 10,
4, c(4, 3, 6, 4), 3, c(3, 5, 4, 2, 2, 6, 3), c(2, 4,
7, 6, 6, 6), c(10, 4, 6, 4, 5, 5, 5, 6), c(11, 15, 12,
9, 15, 8, 25), c(2, 1, 1, 1, 1, 1, 2, 1), c(23, 24, 12,
14, 20, 17, 13, 6, 18), c(18, 13, 19, 42, 10, 12, 21,
27, 7, 7), c(2, 2, 2, 1, 4), c(1, 5, 1, 7, 3, 3), c(6,
6, 18, 8, 6, 14, 16, 3, 7, 5, 4), c(8, 3, 1, 2, 2, 1,
1, 3, 1), c(1, 1, 1, 2, 6, 2), c(1, 1, 2, 2, 2, 1, 1),
c(12, 3, 6, 9, 9, 4)), Brambling = list(c(2, 2), NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, c(2,
2, 2, 2), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, c(1, 2, 2, 1), NULL, NULL, 1,
NULL, NULL, NULL, 1), Blackcap = list(NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, 1, NULL, c(1, 2, 2), NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1, NULL, c(1, 1, 1), NULL, NULL, 1, 1, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, 1, NULL, NULL, NULL, NULL, NULL,
NULL, 1, NULL, NULL, NULL, NULL, NULL, 1, NULL), Jackdaw = list(
2, c(1, 2), NULL, NULL, NULL, NULL, NULL, NULL, c(1,
1, 1, 2), NULL, c(1, 4, 1, 1, 4, 1, 1), 1, NULL, NULL,
c(6, 5, 5, 5, 5, 5), c(4, 2, 4, 1, 5, 1, 5, 1), c(7,
2, 5), c(1, 1, 1, 1), 1, NULL, NULL, NULL, NULL, c(4,
4), NULL, 2, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, c(2, 1), NULL, c(3, 5, 1, 2, 2), c(3, 2, 1, 1,
2, 4, 3, 3), c(3, 3, 6, 2, 9, 4, 10, 3, 2), c(6, 1, 1,
3, 4, 2, 3, 1), c(5, 3, 5, 4, 5, 4, 4), c(3, 2, 6, 5,
2, 3, 1, 3, 3, 4), c(6, 3, 2, 6, 2, 2, 3, 3, 3, 5, 5,
3), c(6, 5, 6, 5, 5, 8, 5, 4, 7, 6), c(5, 2, 3, 5, 4,
3, 3, 5, 2), c(3, 1, 2, 4, 2, 3, 1, 2), c(3, 5, 9, 4,
3, 5, 5, 5, 6, 5, 4, 5, 5), c(5, 1, 8, 6, 5, 6, 3, 3,
8, 6, 4), c(7, 6, 6, 6, 6, 5, 4, 3), c(3, 4, 2, 4, 2,
2, 2, 7, 11, 3, 6)), Siskin = list(NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, c(2, 2, 2),
NULL, NULL, NULL, NULL, NULL, NULL, NULL, c(1, 1), NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, c(2, 3), c(4,
2, 1, 2, 1), NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1, c(2, 2, 2), 1, NULL, c(3, 3), 2, c(4, 1, 2, 3), c(1,
4, 2), c(1, 1, 1, 1, 2), c(3, 3), NULL, c(2, 2, 1), c(5,
1, 2, 2, 2, 2, 2, 2), NULL), `Spotted Flycatcher` = list(
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, 2, NULL, 1, NULL, NULL, NULL, NULL, NULL, c(1,
1), NULL, c(1, 1, 1), NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL)), row.names = c(NA, -50L
), class = c("tbl_df", "tbl", "data.frame"))
这就是您需要的吗?
library(dplyr)
library(purrr) # map_dbl
group_by(zz, year, Season, POSTCODE, LOC_ID) %>%
summarize_all(~ map_dbl(., sum, na.rm = TRUE)) %>%
ungroup()
# # A tibble: 50 x 25
# year Season POSTCODE LOC_ID Wren Dunnock `Blue Tit` `Pied/White Wag~ `Collared Dove` `Great Tit` Robin Greenfinch
# <dbl> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 2010 Summer PE32 1TL LOC56~ 1 10 13 0 62 8 5 30
# 2 2010 Winter PE32 1TL LOC56~ 4 15 16 4 48 10 11 11
# 3 2011 Spring PE32 1TL LOC56~ 0 8 10 1 25 10 18 16
# 4 2011 Summer PE32 1TL LOC56~ 0 18 17 0 52 11 7 17
# 5 2011 Winter PE32 1TL LOC56~ 2 5 22 1 51 10 13 7
# 6 2012 Autumn PE32 1TL LOC56~ 2 6 28 1 24 18 4 9
# 7 2012 Winter PE32 1TL LOC56~ 4 10 18 6 43 16 6 22
# 8 2013 Winter PE32 1TL LOC56~ 2 6 20 2 23 8 5 14
# 9 2014 Autumn NR15 1TS LOC11~ 0 1 30 0 1 6 6 4
# 10 2014 Summer NR15 1TS LOC11~ 0 2 30 0 0 14 1 4
# # ... with 40 more rows, and 13 more variables: `House Sparrow` <dbl>, `Coal Tit` <dbl>, Woodpigeon <dbl>,
# # Blackbird <dbl>, `Song Thrush` <dbl>, Chaffinch <dbl>, Starling <dbl>, Goldfinch <dbl>, Brambling <dbl>,
# # Blackcap <dbl>, Jackdaw <dbl>, Siskin <dbl>, `Spotted Flycatcher` <dbl>
(你可以在没有purrr::map_dbl
的情况下完成,只需在它的位置使用sapply
即可。(
我们可以将summarise
与across
一起使用
library(dplyr)
library(purrr)
zz %>%
group_by(year, Season, POSTCODE, LOC_ID) %>%
summarise(across(everything(), ~ map_dbl(., sum, na.rm = TRUE)))