Is there a better way to write this cumulative sum for a time series?
Given the following data:
sample <- xts(c( 1,1,1,1,1,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,1,1,1,1,1,3,3,3,3,3,3,4,4,4,4,4,4,2,2,1,1,1,1,4,4,4,4,4,4,4,4,4),
as.Date(x = "2014-11-03")+1:52)
I want to create the following:
[,1]
2014-11-05 0
2014-11-06 0
2014-11-07 0
2014-11-08 0
2014-11-09 1
2014-11-10 2
2014-11-11 3
2014-11-12 4
2014-11-13 5
2014-11-14 6
2014-11-15 7
2014-11-16 8
2014-11-17 9
2014-11-18 10
2014-11-19 11
2014-11-20 12
2014-11-21 13
2014-11-22 14
2014-11-23 15
2014-11-24 0
2014-11-25 0
2014-11-26 0
2014-11-27 0
2014-11-28 0
2014-11-29 1
2014-11-30 2
2014-12-01 3
2014-12-02 4
2014-12-03 5
2014-12-04 6
2014-12-05 7
2014-12-06 8
2014-12-07 9
2014-12-08 10
2014-12-09 11
2014-12-10 12
2014-12-11 1
2014-12-12 2
2014-12-13 0
2014-12-14 0
2014-12-15 0
2014-12-16 0
2014-12-17 1
2014-12-18 2
2014-12-19 3
2014-12-20 4
2014-12-21 5
2014-12-22 6
2014-12-23 7
2014-12-24 8
2014-12-25 9
In other words, given a sequence of positive integers, I would like to make a cumulative sum starting from where the observation is not 1, and keep increasing it until the observation value decreases from the previous observation.
This is what I came up with:
require('xts')
sample <- xts(c( 1,1,1,1,1,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,1,1,1,1,1,3,3,3,3,3,3,4,4,4,4,4,4,2,2,1,1,1,1,4,4,4,4,4,4,4,4,4),
as.Date(x = "2014-11-03")+1:52)
# a vector of endpoints
ep <- c(1,which(diff(lag(sample,k=-1))<0),length(sample))
res <- period.apply(sample,ep,function(x){
# Make the 1s into 0s
x[x==1]=0
# Make those that are not 0s into 1s
x[x!=0] = 1
# Now cumsum will give the desired results
cumsum(x)
})
res <- Reduce(rbind,res)
res
Is there a better way to rewrite this? In particular, is it okay to always put the first and last indices at the endpoints, and can the function in period.apply()
be rewritten in a more concise way?
+3
source to share