代码之家  ›  专栏  ›  技术社区  ›  hannes101

从事件指示器变量计算到事件的时间

  •  1
  • hannes101  · 技术社区  · 7 年前

    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
    

    基于此,我想再增加一个专栏 Time.to.Event ,这是相对于事件发生的时间点的变量。所以2003年的变量是-1,2002年是-2,2001年是-3,2000年是-4。 结果data.table应如下所示:

        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
    
    2 回复  |  直到 7 年前
        1
  •  3
  •   IceCreamToucan    7 年前
    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
    
        2
  •  3
  •   Jaap    7 年前

    另一种选择:

    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