1

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)

Maximilian
  • 4,177
  • 7
  • 46
  • 85
  • I don't understand what this question is asking at all. – David Robinson Aug 31 '15 at 14:44
  • 2
    I think `tabulate` strips off the 0 values. Try with `table` – akrun Aug 31 '15 at 14:45
  • @David: please see here: http://stackoverflow.com/questions/17820752/more-elegant-way-to-return-a-sequence-of-numbers-based-on-booleans/17820865#17820865 – Maximilian Aug 31 '15 at 14:46
  • 1
    `length(c(rep(NA,sum(is.na(z))), sequence(table(cumsum(!g))))) #[1] 500` – akrun Aug 31 '15 at 14:47
  • @akrun: yes, that's it, thank you! I thought about trying `table` but I didn't :) – Maximilian Aug 31 '15 at 14:47
  • 1
    @akrun: I'm sorry, I just thought I delete this question due to easy fix. So please submit your answer and I will accept it. Maybe if you could include this-and-that, perhaps useful to some. Thanks. – Maximilian Aug 31 '15 at 15:51

1 Answers1

1

If we look at the ?tabulate

bin: a numeric vector (of positive integers), or a factor. Long vectors are supported.

So, either we convert to factor as the positive integers won't include 0 or we can use table which don't have that problem.

 s1 <- length(c(rep(NA,sum(is.na(z))), sequence(tabulate(factor(cumsum(!g))))))
 s1
 #[1] 500

Or using table

 s2 <- length(c(rep(NA,sum(is.na(z))), sequence(table(cumsum(!g)))))
 s2
 #[1] 500

which is equal to length of 'x'

length(x)
#[1] 500
akrun
  • 874,273
  • 37
  • 540
  • 662