3

我有一个data.table这样的

library(data.table)
df = data.table(
  id = c(rep(1, 3), rep(2, 4), rep(3, 2)),
  time = c(seq(1, 3, 1), seq(1, 4, 1), seq(3, 4)),
  value1 = c(0, 0, 0, 0, 2, 0, 0, 0, 1),
  value2 = c(0, 1, 0, 1, 0, 0, 0, 0, 1)
)

像这样打印

    id   time   value1 value2
1:  1    1      0      0
2:  1    2      0      1
3:  1    3      0      0
4:  2    1      0      1
5:  2    2      2      0
6:  2    3      0      0
7:  2    4      0      0
8:  3    3      0      0
9:  3    4      1      1

现在我想创建两个新列来跟踪每个值列中最后一个非零值的时间id。IE。我想要一个结果数据表如下

    id   time   value1  last_change1  value2  last_change2
1:  1    1      0       NA            0       NA
2:  1    2      0       NA            1       0
3:  1    3      0       NA            0       1
4:  2    1      0       NA            1       0
5:  2    2      2       0             0       1
6:  2    3      0       1             0       2
7:  2    4      0       2             0       3
8:  3    3      0       NA            0       NA
9:  3    4      1       0             1       0

有没有人有一个很好的解决方案,表现也很好?

4

2 回答 2

3

一种选择是用于在列中的相应行中填充占位符中zoo::na.locf最后一个值的行号(特定于组)。最后,从当前行号中减去最后一个(分组方式,例如)。non-zero0valuerow-numbernon-zero.I-.I[1]+1

library(data.table)
library(zoo)

df[, c("last_change1", "last_change2") := 
     lapply(.SD, function(x){as.integer((.I-.I[1]+1) - na.locf(as.integer(ifelse(x == 0, NA_integer_, .I-.I[1]+1)), na.rm = FALSE))}),
   .SDcols = value1:value2, by=id]

已编辑:根据来自的建议添加了简化版本@DavidArenburg

df[, c("last_change1", "last_change2") := 
   lapply(.SD, function(x){.I - na.locf(ifelse(x == 0, NA_integer_, .I), na.rm = FALSE)}),
   .SDcols = value1:value2, by=id]


#Modified df
df
#    id time value1 value2 last_change1 last_change2
# 1:  1    1      0      0           NA           NA
# 2:  1    2      0      1           NA            0
# 3:  1    3      0      0           NA            1
# 4:  2    1      0      1           NA            0
# 5:  2    2      2      0            0            1
# 6:  2    3      0      0            1            2
# 7:  2    4      0      0            2            3
# 8:  3    3      0      0           NA           NA
# 9:  3    4      1      1            0            0
于 2018-05-28T23:14:49.227 回答
1

data.table解决方案:现在可以使用尽可能多的非零值。感谢@DavidArenburg 指出。我不得不说这个例子应该考虑到这种情况。

fun1 <- function(x) {
    split(x,cumsum(x)) %>% lapply(function(x) {
    if(any(x!=0)){ IND2<-(min(which(x!=0)):length(x));x<-NA;x[IND2]<-0:(length(IND2)-1);return(as.numeric(x))} else {x[]<-NA;return(as.numeric(x))}
    }) %>% unlist %>% as.numeric
}


df[,`:=`(last_change1 = fun1(value1), last_change2 = fun1(value2)),by="id"]

结果:

   id time value1 value2 last_change1 last_change2
1:  1    1      0      0           NA           NA
2:  1    2      0      1           NA            0
3:  1    3      0      0           NA            1
4:  2    1      0      1           NA            0
5:  2    2      2      0            0            1
6:  2    3      0      0            1            2
7:  2    4      0      0            2            3
8:  3    3      0      0           NA           NA
9:  3    4      1      1            0            0
于 2018-05-28T15:05:24.590 回答