简体   繁体   English

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

[英]How can I analyze the consecutive persistence of signals (1s and 0s) in dataset in R?

Let's say we have a data set that reflects a series of binary signals--either "1" or "0" over some time period. 假设我们有一个数据集,该数据集反映了一段时间内的一系列二进制信号-“ 1”或“ 0”。 The goal is to measure the consecutive instances of 1, and sum the total. 目的是测量1的连续实例,并对总数求和。 So, for instance, if 1 occurred four times in a row, the cumulative signal would be 4 for the fourth time period. 因此,例如,如果连续发生四次1,则第四时间段的累计信号将为4。 If the next period--the fifth period--was 0, the counter resets until/if the 1 signal returns. 如果下一个周期(第五个周期)为0,则计数器将重置,直到/如果返回1信号。 I'm looking for a way to record the equivalent of rolling frequencies of consecutive instances of 1s. 我正在寻找一种方法来记录连续的1s实例的滚动频率。 I've tried for loops and apply(), but can't seem to find the right mix. 我已经尝试过循环和apply(),但似乎找不到正确的组合。 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? 关于如何使用R解决此问题的任何想法? 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

Another approach using ave : 另一种使用ave方法:

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

The result: 结果:

           ..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
.
.
.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM