CindyLhasapoo - 7 months ago 37

R Question

I would like to count how many times I see two consecutive TRUE values in R. For example,

`x <- c(T,F,T,T,F,F,T,F,T,F)`

x

[1] TRUE FALSE TRUE TRUE FALSE FALSE TRUE FALSE TRUE FALSE

It would count 1 since there is a TRUE at position 3 and TRUE at position 4. If there are more than 2 consecutive TRUE, then I just want to count it only once, ie this vector

`x <- c(T,F,T,T,T,F,T,F,T,F)`

x

[1] TRUE FALSE TRUE TRUE TRUE FALSE TRUE FALSE TRUE FALSE

would still count 1. I started with looking at

`rle()`

Answer

This should work:

```
with(rle(x), sum(lengths[values] >= 2))
```

**Explanation:**

As you are using Booleans, you can take profit of it. `rle(x)$lengths`

will return how many consecutive times `TRUE`

or `FALSE`

happen in the vector. Example

```
x <- c(T,F,T,T,T,F,T,F,T,F,T,T)
rle(x)$lengths
[1] 1 1 3 1 1 1 1 1 2
```

Now you only want those values in this vector that correspond to `TRUE`

s. `rle(x)$values`

returns a vector with the order of appearance. Example:

```
rle(x)$values
[1] TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE
```

You can use this to only get the `TRUE`

s in the `lengths`

vector:

```
rle(x)$lengths[rle(x)$values]
[1] 1 3 1 1 2
```

And the last step should be obvious: count how many of this values are grater or equal than 2. All together (with performance improvement):

```
with(rle(x), sum(lengths[values] >= 2))
[1] 2
```

Source (Stackoverflow)