R:基于条件

时间:2018-04-08 00:35:59

标签: r

我有一个数据集(在此网站的帮助下)根据3个实例创建了一个计数:

  1. Plans大于20
  2. Coins个数
  3. Plans等于20
  4. Coins个数
  5. Plans小于20的Coins的数量
  6. 以下是来自更大数据集的样本

    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)
    

    获得:

    1. C,D - 2
    2. A,F,G,H,J - 5
    3. B,E,I - 3
    4. 我现在想要根据上述三个条件参与Enrollment列和总和。因此,我想达到以下几点:

      1. 400(50 + 350)
      2. 1900(200 + 150 + 200 + 800 + 550)
      3. 750(100 + 400 + 250)

1 个答案:

答案 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)