-1

I have a data table of binary signal bursts over time. Those signals bursts are of random length. So I need to find the row index where a change from 0 to 1 (the start point), and the row index where a change of 1 to 0 (the end point) takes place. So that eventually I can find the start and end time of each signal burst.

How would I do that?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

2

Assuming your data looks like this:

R> x
      V1 V2 V3 V4 V5
 [1,]  0  0  0  0  1
 [2,]  0  1  0  0  1
 [3,]  1  1  0  0  1
 [4,]  1  1  0  0  1
 [5,]  1  1  1  0  1
 [6,]  1  1  1  0  1
 [7,]  1  1  1  0  0
 [8,]  1  1  1  0  0
 [9,]  1  1  1  0  0
[10,]  1  1  1  1  0
[11,]  1  0  1  1  0
[12,]  1  0  1  1  0
[13,]  1  0  1  1  0
[14,]  1  0  1  1  0
[15,]  1  0  1  1  0
[16,]  1  0  1  1  0
[17,]  1  0  1  1  0
[18,]  0  0  1  1  0
[19,]  0  0  1  0  0
[20,]  0  0  1  0  0

I will do this:

apply(x, 2,
      function (k) {
        w <- which(k == 1, arr.ind=TRUE)
        c(head(w, 1), tail(w, 1))
      })

    V1 V2 V3 V4 V5
[1,]  3  2  5 10  1
[2,] 17 10 20 18  6
asb
  • 4,392
  • 1
  • 20
  • 30
1

You can use diff or rle as mentioned in the comment. But you should provide :

  1. a signal example

     set.seed(1)
     rr <- rbinom(30,1,0.5)
    
  2. What have you tried? Using diff for example I do the following

    ind <- c(0,diff(rr))
    
  3. The expected output?

    start <- min(which(ind==1)) ##  change from 0 to 1 (the start point)
    end <- max(which(ind==-1))  ##  change of 1 to 0 (the end point)
    
agstudy
  • 119,832
  • 17
  • 199
  • 261