我正在尝试创建一个 summarise
/filter
dplyr
管道,其等效于以下内容:
iris %>%
mutate(Sepal.Area = Sepal.Length * Sepal.Width,
Petal.Area = Petal.Length * Petal.Width) %>%
group_by(Species) %>%
filter(Sepal.Area < 17) %>%
tally() %>%
filter(Sepal.Area > 17 & Sepal.Area < 22) %>%
tally() %>%
filter(Sepal.Area > 22) %>%
tally()
或者另一种可能的方法:
iris %>%
mutate(Sepal.Area = Sepal.Length * Sepal.Width,
Petal.Area = Petal.Length * Petal.Width) %>%
group_by(Species) %>%
summarise(n(Sepal.Area < 17),
n(Sepal.Area > 17 & Sepal.Area < 22),
n(Sepal.Area > 22))
通过分组中的多个过滤器获取计数的最简单方法是什么? 或者只是运行每个并稍后加入它们?
最佳答案
你可以尝试剪切
:
iris %>%
mutate(Sepal.Area = Sepal.Length * Sepal.Width,
Petal.Area = Petal.Length * Petal.Width,
range = cut(Sepal.Area, breaks = c(0, 17, 22, Inf))) %>%
group_by(Species, range) %>%
summarize(count = n())
# Species range count
# <fctr> <fctr> <int>
# 1 setosa (0,17] 25
# 2 setosa (17,22] 22
# 3 setosa (22,Inf] 3
# 4 versicolor (0,17] 28
# 5 versicolor (17,22] 21
# 6 versicolor (22,Inf] 1
# 7 virginica (0,17] 10
# 8 virginica (17,22] 31
# 9 virginica (22,Inf] 9
关于r - Dplyr 计数/多个过滤器计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43096225/