R 配置数据 Data.Table

R Configure Data With Data.Table

data=data.frame("Student"=c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5),
       "Grade"=c(5,6,7,3,4,5,4,5,6,8,9,10,2,3,4),
       "Pass"=c(NA,0,1,0,1,1,0,1,0,0,NA,NA,0,0,0),
       "NEWPass"=c(0,0,1,0,1,1,0,1,1,0,0,0,0,0,0),
       "GradeNEWPass"=c(7,7,7,4,4,4,5,5,5,10,10,10,4,4,4),
       "GradeBeforeNEWPass"=c(6,6,6,3,3,3,4,4,4,10,10,10,4,4,4))

我有一个data.frame叫数据。它有列名 Student、Grade 和 Pass。我想这样做:

NEWPass:通过并为每个学生填写 NA 值和以前的值。如果第一个值是 'NA' 则输入零。那么这应该是 运行 最大值。

GradeNEWPass:取学生在 NEWPass 中获得的最低等级值。如果一个学生没有在 NEWPass 中获得一个,这等于最高等级。

GradeBeforeNEWPass:在学生获得 NEWPass 之前获取 Grade 的值。如果一个学生没有在 NEWPass 中获得一个,这等于最高等级。

__ 尝试次数:

setDT(data)[, NEWPassTry := cummax(Pass), by = Student]
data$GradeNEWPass = data$NEWPassTry * data$Grade
data[, GradeNEWPass := min(GradeNEWPass), by = Student]

诚然,这并不漂亮,但您的逻辑包含 "if any ... for a student" 之类的词,因此它是分组条件,而不是行条件。

library(magrittr) # just for %>% for breakout, not required
mydata %>%
  .[, NEWPass2 :=
        cummax(fifelse(seq_len(.N) == 1 & is.na(Pass), 0,
                       zoo::na.locf(Pass, na.rm = FALSE))), by = .(Student) ] %>%
  .[, GradeNEWPass2 :=
        if (any(NEWPass2 > 0)) min(Grade[ NEWPass2 > 0 ]) else max(Grade),
    by = .(Student) ] %>%
  .[, GradeBeforeNEWPass2 :=
        if (NEWPass2[1] == 0 && any(NEWPass2 > 0)) Grade[ which(NEWPass2 > 0)[1] - 1 ] else max(Grade),
    by = .(Student) ]
#     Student Grade Pass NEWPass GradeNEWPass GradeBeforeNEWPass NEWPass2 GradeNEWPass2 GradeBeforeNEWPass2
#  1:       1     5   NA       0            7                  6        0             7                   6
#  2:       1     6    0       0            7                  6        0             7                   6
#  3:       1     7    1       1            7                  6        1             7                   6
#  4:       2     3    0       0            4                  3        0             4                   3
#  5:       2     4    1       1            4                  3        1             4                   3
#  6:       2     5    1       1            4                  3        1             4                   3
#  7:       3     4    0       0            5                  4        0             5                   4
#  8:       3     5    1       1            5                  4        1             5                   4
#  9:       3     6    0       1            5                  4        1             5                   4
# 10:       4     8    0       0           10                 10        0            10                  10
# 11:       4     9   NA       0           10                 10        0            10                  10
# 12:       4    10   NA       0           10                 10        0            10                  10
# 13:       5     2    0       0            4                  4        0             4                   4
# 14:       5     3    0       0            4                  4        0             4                   4
# 15:       5     4    0       0            4                  4        0             4                   4

我使用 magrittr::%>% 只是为了将其分解为多个计算阶段,这不是必需的。

我们可以使用data.table方法。按 'Student' 分组,创建索引 ('i1'),其中 'Pass' 为 1 而不是 NA,然后使用 which 和 [=13= 获取 1 的第一个位置] ('i2'),同时计算'Grade' ('mx')的max,然后根据索引创建三列('v1' - 得到累计二进制的最大值,'v2' - ifany 1,然后用索引 'i2' 或 else [=33] 对 'Grade' 进行子集化=] 'mx',同理'v3'-索引减1得到'Grade'值

library(data.table)    
setDT(data)[, c('NEWPass1', 'GradeNEWPass1', 'GradeBeforeNEWPass1') :={
              i1 <- Pass == 1 & !is.na(Pass)
              i2 <- head(which(i1), 1)
              mx <- max(Grade, na.rm = TRUE)
              v1 <- cummax(+(i1))
              v2 <- if(any(i1)) Grade[i2] else mx
              v3 <- if(any(i1)) Grade[max(1, i2-1)] else mx

            .(v1, v2, v3)}, Student]


data
#    Student Grade Pass NEWPass GradeNEWPass GradeBeforeNEWPass NEWPass1 GradeNEWPass1 GradeBeforeNEWPass1
# 1:       1     5   NA       0            7                  6        0             7                   6
# 2:       1     6    0       0            7                  6        0             7                   6
# 3:       1     7    1       1            7                  6        1             7                   6
# 4:       2     3    0       0            4                  3        0             4                   3
# 5:       2     4    1       1            4                  3        1             4                   3
# 6:       2     5    1       1            4                  3        1             4                   3
# 7:       3     4    0       0            5                  4        0             5                   4
# 8:       3     5    1       1            5                  4        1             5                   4
# 9:       3     6    0       1            5                  4        1             5                   4
#10:       4     8    0       0           10                 10        0            10                  10
#11:       4     9   NA       0           10                 10        0            10                  10
#12:       4    10   NA       0           10                 10        0            10                  10
#13:       5     2    0       0            4                  4        0             4                   4
#14:       5     3    0       0            4                  4        0             4                   4
#15:       5     4    0       0            4                  4        0             4                   4