首页 > 解决方案 > Find percentage exceedances by group in R?

问题描述

I've got a data set with some missing values and I want to find percentage exceedances over 500,200, median and 95th percentile of Ecoli for each group?

Site    Ecoli
A   234
A   450
A   500
A   1096
A   295
A   4567
A   2344
A   234
A   450
B   500
B   1096
B   295
B   4567
B   2344
B   
B   
B   
B   

I started with this codes

table=dat %>% group_by(Site) %>% mutate(E.Coli>500)

标签: r

解决方案


After grouping by 'Site', we can use mean on a logical condition to convert it to percentages. If the intention is to create columns, use mutate instead of summarise after the group_by

library(dplyr)
dat %>%
     group_by(Site) %>%
     summarise(PercExeedover500 = 100 *mean(E.Coli > 500, na.rm = TRUE),
               PercExeedover200 = 100 *mean(E.Coli > 200, na.rm = TRUE),
                Quantile95th = quantile(Ecoli, prob = .95, na.rm = TRUE))

推荐阅读