如何分析R中数据集中的信号(1s和0s)的连续持久性?

问题描述:

比方说,我们有一个反映一组二进制信号的数据集,这些信号在某个时间段内为"1"或"0".目标是测量1的连续实例,并对总数求和.因此,例如,如果连续发生四次1,则第四时间段的累积信号将为4.如果下一个周期(第五个周期)为0,则计数器将重置,直到/如果返回1信号.我正在寻找一种方法来记录连续的1s实例的滚动频率.我已经尝试过循环和apply(),但似乎找不到正确的组合.任何建议将不胜感激.作为测试,让我们创建一些虚假数据.关于如何使用R解决此问题的任何想法?谢谢!

Let's say we have a data set that reflects a series of binary signals--either "1" or "0" over some time period. The goal is to measure the consecutive instances of 1, and sum the total. So, for instance, if 1 occurred four times in a row, the cumulative signal would be 4 for the fourth time period. If the next period--the fifth period--was 0, the counter resets until/if the 1 signal returns. I'm looking for a way to record the equivalent of rolling frequencies of consecutive instances of 1s. I've tried for loops and apply(), but can't seem to find the right mix. Any suggestions would be greatly appreciated. As a test, let's create some fake data. Any ideas on how to use R to solve this question? Thanks!

 library(xts)
 set.seed(5)
 x <- xts(cbind(sample(0:1,50,replace=T)), order=Sys.Date()-100 + 1:50)

 > head(x)
       [,1]
 2014-08-26    0
 2014-08-27    1
 2014-08-28    1
 2014-08-29    0
 2014-08-30    0
 2014-08-31    1

使用ave的另一种方法:

cbind(x, newCol = ave(x, cumsum(c(FALSE, (as.logical(diff(x)))[-1])), 
                      FUN = function(i) seq_along(i) * i))

结果:

           ..1 newCol
2014-08-26   0      0
2014-08-27   1      1
2014-08-28   1      2
2014-08-29   0      0
2014-08-30   0      0
2014-08-31   1      1
2014-09-01   1      2
2014-09-02   1      3
2014-09-03   1      4
2014-09-04   0      0
.
.
.