r - 在 R (dplyr) 中重置的条件运行计数(累计和)

标签 r dplyr conditional cumulative-sum

我正在尝试计算以其他变量为条件的运行计数(即累积总和),并且可以为另一个变量的特定值重置。我在 R 工作,更喜欢 dplyr如果可能,基于解决方案。

我想为运行计数创建一个变量,cumulative ,基于以下算法:

  • 计算 cumulative 组合内的运行计数 ( id )和 age
  • 对于每个后续 cumulative,将运行计数 ( trial ) 增加 1哪里accuracy = 0 , block = 2 , 和 condition = 1
  • 对于每个 cumulative,将运行计数 ( trial ) 重置为 0哪里accuracy = 1 , block = 2 , 和 condition = 1 , 下一个增量从 1(不是前一个数字)开始
  • 每个trial哪里block != 2 , 或 condition != 1 , 将运行计数 ( cumulative ) 保留为 NA

  • 这是一个最小的工作示例:
    mydata <- data.frame(id = c(1,1,1,1,1,1,1,1,1,1,1),
                     age = c(1,1,1,1,1,1,1,1,1,1,2),
                     block = c(1,1,2,2,2,2,2,2,2,2,2),
                     trial = c(1,2,1,2,3,4,5,6,7,8,1),
                     condition = c(1,1,1,1,1,2,1,1,1,1,1),
                     accuracy = c(0,0,0,0,0,0,0,1,0,0,0)
    )
    
    id  age block   trial   condition   accuracy
    1   1   1       1       1           0
    1   1   1       2       1           0
    1   1   2       1       1           0
    1   1   2       2       1           0
    1   1   2       3       1           0
    1   1   2       4       2           0
    1   1   2       5       1           0
    1   1   2       6       1           1
    1   1   2       7       1           0
    1   1   2       8       1           0
    1   2   2       1       1           0
    

    预期的输出是:
    id  age block   trial   condition   accuracy    cumulative
    1   1   1       1       1           0           NA
    1   1   1       2       1           0           NA
    1   1   2       1       1           0           1
    1   1   2       2       1           0           2
    1   1   2       3       1           0           3
    1   1   2       4       2           0           NA
    1   1   2       5       1           0           4
    1   1   2       6       1           1           0
    1   1   2       7       1           0           1
    1   1   2       8       1           0           2
    1   2   2       1       1           0           1
    

    最佳答案

    这是一个使用 data.table 的选项.基于 match 创建一个二进制列正在paste d 'accuracy', 'block', 'condition' 的值与自定义值的值,按二进制列的 run-length-id ('ind'), 'id' 和 'age' 分组,得到累积和'ind' 并将其分配 ( := ) 到一个新列 ('Cumulative')

    library(data.table)
    setDT(mydata)[, ind := match(do.call(paste0, .SD), c("121", "021")) - 1,
        .SDcols = c("accuracy", "block", "condition")
         ][, Cumulative := cumsum(ind), .(rleid(ind), id, age)
          ][, ind := NULL][]
    #    id age block trial condition accuracy Cumulative
    # 1:  1   1     1     1         1        0         NA
    # 2:  1   1     1     2         1        0         NA
    # 3:  1   1     2     1         1        0          1
    # 4:  1   1     2     2         1        0          2
    # 5:  1   1     2     3         1        0          3
    # 6:  1   1     2     4         2        0         NA
    # 7:  1   1     2     5         1        1          0
    # 8:  1   1     2     6         1        0          1
    # 9:  1   1     2     7         1        0          2
    #10:  1   2     2     1         1        0          1
    

    关于r - 在 R (dplyr) 中重置的条件运行计数(累计和),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52960348/

    相关文章:

    php - 在 Twig 中,检查数组的特定键是否存在

    sql - oracle db中具有多个字段的条件唯一约束

    makefile - "ifeq"生成文件中的条件语法

    r - 计算向量中每个值的阶乘

    r - 将列表转换为 R 中的数据框

    r - 来自键值表的动态 if-else "tests"或 case_when "formulas"?

    r - 基于多行重复值的子集数据框

    R:如何使用 dplyr 计算具有缺失值的每一行的平均值

    r - 透明网格线

    r - 如何使用 dplyr 计算 R 中的分组 z 分数?