library(data.table)
dt <- data.table(cbind(id = c(rep("0151", 16), rep("4615", 16)),
date = rep(c("2011-08-09",
"2011-08-10",
"2011-08-12",
"2011-08-14",
"2011-08-15",
"2011-08-16",
"2011-08-17",
"2011-08-18",
"2011-08-19",
"2011-08-20",
"2011-08-23",
"2011-08-24",
"2011-08-27",
"2011-08-28",
"2011-08-30",
"2011-08-31"), 2),
count = c(c(7, 1, 0, 4, 1, 4, 2, 1, 0, 0, 0, 0, 0, 1, 0, 1),
c(0, 1, 3, 0, 1, 0, 5, 1, 0, 0, 5, 0, 1, 2, 0, 1))))
对于每个ID,我正在寻找一种有效计算(并存储在新列中)的方法,从计数的最后一天开始经过了多少天&gt;所以第四列看起来像这样:
c(NA, 1, 2, 4, 1, 1, 1, 1, 1, 2, 5, 6, 9, 10, 2, 3, NA, NA, 2, 2, 3, 1, 2, 1, 1, 2, 5, 1, 4, 1, 2, 3)
答案 0 :(得分:2)
我们可以使用na.locf
中的zoo
来创建一个新的日期列,其中count > 0
向下填充的最后一天的日期为lag
,因为我们没有如果给定日期本身有count > 0
,则不想返回0天。相反,我们希望计算上一个非零count
日期的天数。最后,我们在difftime
和date
:
date2
library(data.table)
library(zoo)
dt[,count2 := difftime(date, na.locf(lag(ifelse(count > 0, date, NA)), na.rm = FALSE)), by = id]
<强>结果:强>
id date count count2
1: 0151 2011-08-09 7 NA days
2: 0151 2011-08-10 1 1 days
3: 0151 2011-08-12 0 2 days
4: 0151 2011-08-14 4 4 days
5: 0151 2011-08-15 1 1 days
6: 0151 2011-08-16 4 1 days
7: 0151 2011-08-17 2 1 days
8: 0151 2011-08-18 1 1 days
9: 0151 2011-08-19 0 1 days
10: 0151 2011-08-20 0 2 days
11: 0151 2011-08-23 0 5 days
12: 0151 2011-08-24 0 6 days
13: 0151 2011-08-27 0 9 days
14: 0151 2011-08-28 1 10 days
15: 0151 2011-08-30 0 2 days
16: 0151 2011-08-31 1 3 days
17: 4615 2011-08-09 0 NA days
18: 4615 2011-08-10 1 NA days
19: 4615 2011-08-12 3 2 days
20: 4615 2011-08-14 0 2 days
21: 4615 2011-08-15 1 3 days
22: 4615 2011-08-16 0 1 days
23: 4615 2011-08-17 5 2 days
24: 4615 2011-08-18 1 1 days
25: 4615 2011-08-19 0 1 days
26: 4615 2011-08-20 0 2 days
27: 4615 2011-08-23 5 5 days
28: 4615 2011-08-24 0 1 days
29: 4615 2011-08-27 1 4 days
30: 4615 2011-08-28 2 1 days
31: 4615 2011-08-30 0 2 days
32: 4615 2011-08-31 1 3 days
id date count count2