我有一个数据集(在此网站的帮助下)根据3个实例创建了一个计数:
Plans
大于20 Coins
个数
Plans
等于20 Coins
个数
Plans
小于20的Coins
的数量以下是来自更大数据集的样本
Plan Year Coins Copay Enrollment
A 2018 20 10 200
B 2014 15 5 100
C 2012 30 0 50
D 2017 30 10 350
E 2018 5 10 400
F 2018 20 0 150
G 2018 20 0 200
H 2016 20 10 800
I 2014 10 3 250
J 2017 20 7 550
这是通过以下方式实现的:
df %>%
group_by(grp = case_when(Coins < 20 ~ 'grp1', Coins ==20 ~ 'grp2', TRUE ~ 'grp3')) %>%
summarise(Plan = toString(unique(Plan)), prop = n()) %>%
ungroup %>%
select(-grp)
获得:
我现在想要根据上述三个条件参与Enrollment
列和总和。因此,我想达到以下几点:
答案 0 :(得分:1)
您可以在counts = sum(Enrollment)
之前添加ungroup
摘要。
library(tidyverse);
df %>%
group_by(grp = case_when(Coins < 20 ~ 'grp1', Coins ==20 ~ 'grp2', TRUE ~ 'grp3')) %>%
group_by(grp) %>%
summarise(
Plan = toString(unique(Plan)),
prop = n(),
counts = sum(Enrollment)) %>%
ungroup() %>%
select(-grp);
## A tibble: 3 x 4
# Plan prop counts
# <chr> <int> <int>
#1 B, E, I 3 750
#2 A, F, G, H, J 5 1900
#3 C, D 2 400
df <- read.table(text =
"Plan Year Coins Copay Enrollment
A 2018 20 10 200
B 2014 15 5 100
C 2012 30 0 50
D 2017 30 10 350
E 2018 5 10 400
F 2018 20 0 150
G 2018 20 0 200
H 2016 20 10 800
I 2014 10 3 250
J 2017 20 7 550", header = T)