I cannot replicate the exact sequence of x
that would generate shorter output s
by about 20
observations, but it does happen. Why is this? How can I fix (assure) that it its length always equal to the base sequence x
? Sometimes x
and s
differ by way more than just 2 observations (due to cumsum
).
Example:
set.seed(123)
# this sequence length is equal (or close):
x <- diff(log(rnorm(500,5,1))); x[1:5] <- NA
# this sequence doesn equal; is shorter as the output `s`
x <- rnorm(500,0.1,0.1); x[1:5] <- NA
z <- ifelse(x<0,FALSE,ifelse(x>0,TRUE,NA))
g <- z[!is.na(z)]
s <- c(rep(NA,sum(is.na(z))), sequence(tabulate(cumsum(!g))))
s
length(x) # check length
length(s) # check length
So the length of output s
is data dependent.
The aim so to add the output s
to x
; data.frame(s,x)