按组计算数据集中各个列的计数和平均值

时间:2019-11-15 18:40:50

标签: r dplyr count average summarize

我正在尝试汇总数据集。

我正在寻找一张包含所有计数和平均值的表格。

示例数据:

df <- data.frame(
    "Species" = c("A","B","C","D","A","B","C","D"), 
    "Location" =  c("A","B","C","B","A","D","D","E"), 
    "Sample size" = c(1,30,6,2,5,10,3,300), 
    "Frequency"=c(0,0.3,80,0.5,0.01,0.6,1,2)
  )

df

数据生成如下表:

     Species Country Sample.size Frequency
1       A       A           1         0
2       B       B          30       0.3
3       C       C           6        80
4       D       B           2       0.5
5       A       A           5      0.01
6       B       D          10       0.6
7       C       D           3         1
8       D       E         300         2

我正在尝试创建一个表格,其中包含一列:物种,物种出现次数的计数,国家/地区数量的计数物种出现时,每个物种的样本量平均值,每个物种的平均值频率。

本质上,我正在尝试获取如下表格:

Species species_count #_of_Countries Avg_Sample.size Avg_Frequency
A       2             2              10              0
B       2             3              3               0.01
C       3             4              1               20
D       5             1              5               0.5

我是R的新手,所以我们将不胜感激!

1 个答案:

答案 0 :(得分:1)

我想这就是你想要的     库(dplyr)

Summary_df <- df %>% 
group_by(species) %>%
summarize(species_count = n(), 
                 country_count = sum(!is.na(Country)), 
             Avg_sample_size = mean(Sample.size), 
             Avg_frequency = mean(Frequency))