I have a vector that looks like
x <- c(1, 4, 7, 9)
I need output as (1,1,1,4,4,4,7,7,9). Repeat 1 three times that is (4-1), repeat 4 three times (7-4) and so on.
x <- c(1, 4, 7, 9)
I have no idea what to do next. I dont want a for loop for this. I know rep() can be used for repeat. But how many times need to be determined from vector itself.
rep(x, c(diff(x), 1))
[1] 1 1 1 4 4 4 7 7 9
cummax(`[<-`(logical(max(x)),x,x))
[1] 1 1 1 4 4 4 7 7 9