【问题标题】:Reverse cumulative sum by groups in R with multiple group variableR中具有多个组变量的组反转累积总和
【发布时间】:2021-09-21 04:03:32
【问题描述】:

我的数据包含三个变量:日期、年龄组和药物的累积剂量。每天有多次观察(每个年龄组一次)。我需要保留原始数据中的行数和变量数,但还要添加第四个变量,表示在相关日期对相关组施用的实际剂量。

我已经尝试了this question 的解决方案,但没有运气。我收到有关引入 NA 的 mutate 函数的警告。代码不会出错,但我在新变量中得到的数字不正确。其中一些是 NA,就像警告所说的那样,有些甚至是负面的。我认为这可能与我认为我需要分组的两个变量都不是数字的事实有关,但我不确定。在使用其他 SO 帖子上的解决方案之前,我尝试将组变量强制为数字,但结果存在相同的问题。

这是一个与我的特征相似的虚拟数据集:

structure(list(test_dates = structure(c(17897, 17897, 17897, 
17897, 17897, 17898, 17898, 17898, 17898, 17898, 17899, 17899, 
17899, 17899, 17899, 17900, 17900, 17900, 17900, 17900, 17901, 
17901, 17901, 17901, 17901, 17902, 17902, 17902, 17902, 17902, 
17903, 17903, 17903, 17903, 17903, 17904, 17904, 17904, 17904, 
17904, 17905, 17905, 17905, 17905, 17905, 17906, 17906, 17906, 
17906, 17906), class = "Date"), test_ages = structure(c(1L, 5L, 
3L, 2L, 4L, 1L, 5L, 3L, 2L, 4L, 1L, 5L, 3L, 2L, 4L, 1L, 5L, 3L, 
2L, 4L, 1L, 5L, 3L, 2L, 4L, 1L, 5L, 3L, 2L, 4L, 1L, 5L, 3L, 2L, 
4L, 1L, 5L, 3L, 2L, 4L, 1L, 5L, 3L, 2L, 4L, 1L, 5L, 3L, 2L, 4L
), .Label = c("<18", "18-29", "30-39", "40-49", "50+"), class = c("ordered", 
"factor")), cumudose = c(50, 200, 300, 400, 20, 60, 220, 317, 
450, 28, 90, 330, 350, 460, 38, 150, 400, 400, 500, 50, 175, 
453, 429, 574, 69, 182, 491, 474, 601, 102, 205, 506, 491, 682, 
176, 235, 516, 568, 821, 199, 250, 525, 596, 850, 260, 294, 533, 
667, 888, 277)), row.names = c(NA, -50L), class = "data.frame")

当前数据框的前 10 行如下所示:

test_dates test_ages cumudose
2019-01-01 <18 50
2019-01-01 50+ 200
2019-01-01 30-39 300
2019-01-01 18-29 400
2019-01-01 40-49 20
2019-01-02 <18 60
2019-01-02 50+ 220
2019-01-02 30-39 317
2019-01-02 18-29 450
2019-01-02 40-49 28

我希望添加新变量后的数据如下所示:

test_dates test_ages cumudose numdose
2019-01-01 <18 50 50
2019-01-01 50+ 200 200
2019-01-01 30-39 300 300
2019-01-01 18-29 400 400
2019-01-01 40-49 20 20
2019-01-02 <18 60 10
2019-01-02 50+ 220 20
2019-01-02 30-39 317 17
2019-01-02 18-29 450 50
2019-01-02 40-49 28 8

如果我可以提供任何其他信息,请告诉我!

【问题讨论】:

标签: r grouping dplyr cumsum


【解决方案1】:

我们可能需要difference

library(dplyr)
out <- df1 %>% 
   group_by(test_ages) %>% 
   mutate(numdose = c(first(cumudose), diff(cumudose))) %>%
   ungroup

-输出

> out
# A tibble: 50 x 4
   test_dates test_ages cumudose numdose
   <date>     <ord>        <dbl>   <dbl>
 1 2019-01-01 <18             50      50
 2 2019-01-01 50+            200     200
 3 2019-01-01 30-39          300     300
 4 2019-01-01 18-29          400     400
 5 2019-01-01 40-49           20      20
 6 2019-01-02 <18             60      10
 7 2019-01-02 50+            220      20
 8 2019-01-02 30-39          317      17
 9 2019-01-02 18-29          450      50
10 2019-01-02 40-49           28       8
# … with 40 more rows

或者做lag和当前值的区别

df1 %>%
   group_by(test_ages) %>% 
   mutate(numdose = coalesce(cumudose  - lag(cumudose), cumudose)) %>%
   ungroup
# A tibble: 50 x 4
   test_dates test_ages cumudose numdose
   <date>     <ord>        <dbl>   <dbl>
 1 2019-01-01 <18             50      50
 2 2019-01-01 50+            200     200
 3 2019-01-01 30-39          300     300
 4 2019-01-01 18-29          400     400
 5 2019-01-01 40-49           20      20
 6 2019-01-02 <18             60      10
 7 2019-01-02 50+            220      20
 8 2019-01-02 30-39          317      17
 9 2019-01-02 18-29          450      50
10 2019-01-02 40-49           28       8
# … with 40 more rows

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-18
    • 2019-09-20
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2018-08-17
    相关资源
    最近更新 更多