R how to identify distance of last occurrence

后端 未结 3 2013
耶瑟儿~
耶瑟儿~ 2021-01-18 08:31

I want to calculate how long its been since something occurred.

Given the following, you can see that the light is on some of the time, but not all of the time. I wa

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-18 09:24

    An approach using run length encoding (rle) and sequence (which is a wrapper for unlist(lapply(nvec, seq_len))

    d[, distance := sequence(rle(light)$lengths)][(light), distance := 0]
    

提交回复
热议问题