r - 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)
解决方案
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))
推荐阅读
- python - 在 python def 参数中传递部分数据框名称
- python - 如何在 SQLAlchemy 中获取关系列的数据?
- amazon-web-services - AWS MSK Kafka 集群在最大大小更改后不会自动扩展
- java - 本机查询 mapstruct
- indexing - 使用 solr 8.9 在索引的所有字段上搜索单词
- android - 什么是读取 Sqlite 数据库时出错:数据库 'LiveSqliteDatabaseId>?
- python - 输出中没有 CSV 文件
- android - Gradle 不会一次性同步库
- flutter - 我想使用疫苗的图标,但添加代码时它不起作用
- java - 如何选择要上传的 PDF 文件