How to obtain a position of last non-zero element

jak*_*kes 19 r base tidyverse

I've got a binary variable representing if event happened or not:

event <- c(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0)
Run Code Online (Sandbox Code Playgroud)

I need to obtain a variable that would indicate the time when the last event happened. The expected output would be:

last_event <- c(0, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 13, 13, 13, 13)
Run Code Online (Sandbox Code Playgroud)

How can I obtain that with base R, tidyverse or any other way?

mgi*_*nti 18

利用您具有二进制向量的事实,以下给出了所需的输出:

cummax(seq_along(event) * event)
Run Code Online (Sandbox Code Playgroud)

  • 是! 比我的解决方案优雅得多。我在考虑累积总和,但没有想到将索引乘以二进制向量。 (6认同)
  • 或不乘以cummax(ifelse(event,seq_along(event),0))` (3认同)

Kon*_*lph 8

每当您需要用值填充重复项时,请考虑游程编码

在这种情况下,您可以确定行程长度,然后count == 0根据次数重复索引:

lengths = rle(event == 0)$lengths
nonzeros = which(event != 0)
runs = c(0, rep(nonzeros, each = 2))
result = rep(runs, lengths)
Run Code Online (Sandbox Code Playgroud)

或者,将运行替换为RLE,然后将其求逆:

rle = rle(event == 0)
nonzeros = which(event != 0)
rle$values = c(0, rep(nonzeros, each = 2))
result = inverse.rle(rle)
Run Code Online (Sandbox Code Playgroud)