0

I have a boolean vector derived from a time series. It basically looks like this: c(true, false, true, true, false, false, false, true, true...).

I want to count the periods in which the vector is true. Example: for a vector like this: c(true, true, false, true) it would be 2 (1 long true period in the beginning, 1 short in the end).

the vector can contain NAs in the beginning.

I thought of something like:

aaa <- c(NA,F,T,T,T,F)    
for(j in 1:(length(aaa)-1)){  
  if(aaa[j]!=aaa[j+1]){  
    # add a counter that returns [1]  
  }  
} 

=================================================

edit: This works fine for me:

data.frame("#"=unlist(rle(aaa)[1]),"length"=unlist(rle(aaa)[2]))
enroute
  • 183
  • 9
  • 2
    What would you expect c(TRUE, NA, TRUE) to give you? The answer to your query's likely to use run length encoding see `?rle` – Miff Oct 18 '16 at 09:58
  • Please edit the post to include you comment and also include dummy code to highlight 1) input vector 2) output vector 3) what have you tried so far. Please do look at [how to post a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Silence Dogood Oct 18 '16 at 09:58
  • @Miff: c(TRUE, NA, TRUE) should return 2 – enroute Oct 18 '16 at 10:09
  • `rle(aaa[!is.na(aaa)])` maybe – Haboryme Oct 18 '16 at 10:21
  • similar to [this one](http://stackoverflow.com/questions/8400901/detect-intervals-of-the-consequent-integer-sequences) if you first convert to index with `which()`. – roman Oct 18 '16 at 10:23

1 Answers1

1

How about this:

aaa[is.na(aaa)] <- FALSE
sum(rle(aaa)$values)

Convert NAs to FALSE, and the see how many TRUE values there are in the run length encoding (TRUE is stored as 1, FALSE as zero).

Miff
  • 7,486
  • 20
  • 20