Count the number of consecutive TRUE values in R

两盒软妹~` 提交于 2020-01-01 19:17:07

问题


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() but I got stuck. Any help would be greatly appreciated. Thanks!


回答1:


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



回答2:


You could also use cumsum and split for that purpose as follows:

x <- c(TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, 
       TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE)
sum(lapply(split(x, cumsum(!x)), sum)>=2)
#[1] 3



回答3:


Here is another option with rleid from data.table. The rleid gives a unique id to adjacent elements that are different, multiply with x, to convert the elements that corresponds to the FALSE in original vector to 0, get the frequency with tabulate convert to a logical vector (==2) and get the sum.

library(data.table)
sum(tabulate(rleid(x)*x)==2)
#[1] 1


来源:https://stackoverflow.com/questions/40854012/count-the-number-of-consecutive-true-values-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!