我有一个数据框,其中包含两个位置的许多物种的丰度数据:
sp1 sp2 sp3 sp4
SiteA 0 12 0 0
SiteA 0 3 0 0
SiteA 1 0 0 0
SiteB 0 0 6 0
SiteB 2 1 1 0
SiteB 0 1 0 8
我想计算两件事:
每个地点发现了多少种物种。在此虚拟示例中,SiteA 有两个物种,SiteB 有四个物种。
每个位点每行中类群的平均数量。在本例中,SiteA 为 1,SiteB 为 2。
我喜欢用dplyr
和tidyverse
此类总结问题的软件包。更多这里:https://dplyr.tidyverse.org/
library(tidyverse)
# First I'd like to reshape into long (aka "tidy") format
df_tidy <- df %>%
mutate(obs_num = row_number()) %>% # To keep track of orig row
gather(sp, count, sp1:sp4)
# First question
df_tidy %>%
# This gives total counts for all recorded combos of site and species
count(site, sp, wt = count) %>%
filter(n > 0) %>%
count(site) # Count how many rows (ie species) for each site
## A tibble: 2 x 2
# site nn
# <chr> <int>
#1 SiteA 2
#2 SiteB 4
# Second question
df_tidy %>%
# Count how many observations had counts > 0 for each site
count(site, obs_num, wt = count > 0) %>%
group_by(site) %>%
summarize(avg_taxa = mean(n))
## A tibble: 2 x 2
# site avg_taxa
# <chr> <dbl>
#1 SiteA 1
#2 SiteB 2
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)