这是尝试使用data.table
.分组方法是在@jeremys的答案,虽然我正在避免ifelse
或lapply
在这里,而是将根据第一个income
值复制的第一个income
值与NA
s值复制.N - (cutoff[1L] + 1L)
时间相结合.我也是第一次使用这些值cutoff > 0L)
library(data.table) setDT(test_case)[which.max(cutoff > 0L):.N, # Or `cutoff > 0L | is.na(income)` income := c(rep(income[1L], cutoff[1L] + 1L), rep(NA, .N - (cutoff[1L] + 1L))), by = cumsum(cutoff != 0L)] test_case # person year income cutoff # 1: 1 2010 4 0 # 2: 1 2011 10 0 # 3: 1 2012 13 2 # 4: 2 2010 13 0 # 5: 2 2011 13 0 # 6: 2 2012 NA 0 # 7: 3 2010 13 3 # 8: 3 2011 13 0 # 9: 3 2013 13 0 # 10: 3 2014 13 0 # 11: 3 2014 NA 0 # 12: 3 2014 NA 0