计算R中出现的百分比

时间:2019-04-09 17:55:24

标签: r dplyr

我对猛禽物种进行了饮食分析,我想计算出在其繁殖周期的三个不同阶段中猎物的出现百分比。我希望将发生率表示为样本量的百分比。例如,如果样本大小为135,而我出现了直翅目65。我想计算百分比:65/135。

到目前为止,我已经尝试了没有成功的长版本。我得到的结果不正确。强烈建议您提供任何帮助,如果重新发布此问题,将深表歉意。

原始数据集如下:

set.seed(123)
pellets_2014<-data.frame(
    Period = sample(c("Prebreeding","Breeding","Postbreedng"),12, replace=TRUE),
    Orthoptera = sample(0:10, 12,replace=TRUE),
    Coleoptera=sample(0:10,12,replace = TRUE),
    Mammalia=sample(0:10,12, replace=TRUE))
##I transform the file to long format
##Library all the necessary packages 
library(dplyr)
library(tidyr)
library(scales)
library(naniar)
pellets2014_long<-gather(pellets_2014,Categories, Count, c(Orthoptera,Coleoptera,Mammalia))

##I trasnform the zero values to NAs

pellets2014_NA<-pellets2014_long %>% replace_with_na(replace = list(Count = 0))

## Try to calculate the occurence 
Occurence2014<-pellets2014_NA %>% 
    group_by(Period,Categories) %>%
    summarise(n=n())

## I do get here but I don't get the right number of occurence and I am stuck how to get the right percentage
##If I try this: 

Occurence2014<-pellets2014_NA %>% 
    group_by(Period,Categories) %>%
    summarise(n=n())%>%mutate(Freq_n=n/sum(n)*100) 

##The above is also wrong because I need it to be divide by the sample size in each period (here is 4 samples per period, the overall sample size is 12)!

输出必须是每个期间内其猎物类别的发生率和发生率。如下图所示

Desired output

2 个答案:

答案 0 :(得分:0)

这与您要寻找的东西接近吗?

Occurence2014 <- pellets2014_NA %>% 
  group_by(Period,Categories) %>%
  summarise(n = n()) %>%
  ungroup() %>% 
  mutate(
    freq = n / sum(n)
  )

答案 1 :(得分:0)

像这样吗?

Occurence2014 <- pellets2014_NA %>% 
 group_by(Period) %>%
 mutate(period_sample_size = n()) %>%
 ungroup() %>%
 group_by(Period,Categories,period_sample_size) %>%
 summarise(n=n())%>%
 mutate(Freq_n=n/period_sample_size*100)