This removes only the leading zeros and leaves in the other zeros:
TimeSeries[cumsum(TimeSeries)!=0]
#[1] 9 10 10 16 7 13 0 9 1 11 2 11 3 11 4 1 20 13 18 19 16 16 16 15 14 27 24 35 8 18 21 20 19 22 18 21
Why is this doing the trick?
The out put of the cumsum
is:
cumsum(TimeSeries)
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 19 29 45 52 65 65 74 75
[33] 86 88 99 102 113 117 118 138 151 169 188 204 220 236 251 265 292 316 351 359 377 398 418 418 437 459 477 498
Thus, only in cases where there are only zeros the result is equal to zero. In case there is a zero somewhere halfway in the time series the cumsum will not change but won't be zero.
If there are negative values in the timeseries you can use:
TimeSeries[cumsum(abs(TimeSeries))!=0]