我想计算事件的相对时间。我有以下MWE:
customer
基于此,我想添加另一列library(data.table)
dt.MWE <- structure(list(Year = 2000:2015, Event = c(0L, 0L, 0L, 0L, 1L,
0L, 0L, 0L, 0L, 0L,
1L, 0L, 0L, 0L, 0L,
0L)), row.names = c(NA, -16L)
, class = c("data.table", "data.frame")
, index = structure(integer(0), "`__Year`" = integer(0)))
Year Event
1: 2000 0
2: 2001 0
3: 2002 0
4: 2003 0
5: 2004 1
6: 2005 0
7: 2006 0
8: 2007 0
9: 2008 0
10: 2009 0
11: 2010 1
12: 2011 0
13: 2012 0
14: 2013 0
15: 2014 0
16: 2015 0
,该列是相对于事件发生时间的变量。因此2003年的变量是-1,2002是-2,2001是-3,2000是-4。
但是,在事件发生后,它应该变为正,当没有将来的事件发生时,例如在2011年,它应该变为1,或者相对于下一个将来的事件,它应该为负。
生成的data.table应该如下所示:
Time.to.Event
答案 0 :(得分:3)
library(data.table)
setDT(dt.MWE)
dt.MWE[, Time.to.Event := seq(.N) - ifelse(any(Event), .N, 0L)
, by = cumsum(Event) - Event]
# Year Event Time.to.Event
# 1: 2000 0 -4
# 2: 2001 0 -3
# 3: 2002 0 -2
# 4: 2003 0 -1
# 5: 2004 1 0
# 6: 2005 0 -5
# 7: 2006 0 -4
# 8: 2007 0 -3
# 9: 2008 0 -2
# 10: 2009 0 -1
# 11: 2010 1 0
# 12: 2011 0 1
# 13: 2012 0 2
# 14: 2013 0 3
# 15: 2014 0 4
# 16: 2015 0 5
答案 1 :(得分:3)
另一个选择:
dt.MWE[, Time.to.Event := (1:.N) - c(0,.N)[sum(Event) + 1]
, by = cumsum(shift(Event, fill = 0))][]
给出:
> dt.MWE Year Event Time.to.Event 1: 2000 0 -4 2: 2001 0 -3 3: 2002 0 -2 4: 2003 0 -1 5: 2004 1 0 6: 2005 0 -5 7: 2006 0 -4 8: 2007 0 -3 9: 2008 0 -2 10: 2009 0 -1 11: 2010 1 0 12: 2011 0 1 13: 2012 0 2 14: 2013 0 3 15: 2014 0 4 16: 2015 0 5