Identifying blocks of TRUEs in list

boolean, list, logical-operators, r

Solution

You could use `rle` and do something like:

myL = c(TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, TRUE, TRUE)
num = c(1, 4, 3, 7, 3, 2 , 23, 98, 5)
X <- rle(myL)
Y <- rep(seq_along(X$lengths)[X$values], X$lengths[X$values])
tapply(num[myL], Y, sum)
  1   3   5 
  8   2 103 

Here, the first group of `TRUE`s sums to 8, the second group (one value) is 2, and the third group (98 and 5) sum to 103.

Alternatively, install my "SOfun" package from GitHub, which has a function called `TrueSeq` that would be useful for this.

Install it with:

library(devtools)
install_github("mrdwab/SOfun")

The function does this:

TrueSeq(myL)
# [1] 1 1 1 0 0 2 0 3 3

This allows you to simply do:

tapply(num, TrueSeq(myL), sum)
#   0   1   2   3 
#  33   8   2 103 

Or, even better, use the `zero2NA` argument in `TrueSeq`, like this:

tapply(num, TrueSeq(myL, zero2NA=TRUE), sum)
#   1   2   3 
#   8   2 103

Problem

I'm doing some analysis stuff and using R. I encountered a problem: I have a list of logicals, which looks as follows : ``` list = (TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, TRUE, TRUE) ``` for every logical value a have a list of numeric values : ``` num = (1, 4, 3, 7, 3, 2 , 23, 98, 5) ``` I would like to sum the values for the blocks of TRUEs and put them in the matrix or sth. Just like that : `list[1] == TRUE and list[2] == TRUE and list[3] == TRUE` so i'm calculating the sum : 1+4+3 = 8 so `matrix[1,1] <- 8` and then omit list[4] and list[5] because it's FALSE and go to list[6], `matrix[2,1] <- 2` ... and so on... I have no idea how to extract those blocks of TRUEs. I hope you can help me somehow!

Original source