kf007
kf007

Reputation: 117

Determine the number of runs of a specified length

I have the following vector, data.

data <- c(1,1,0,1,1,1,0,1,0,0,0,0,0,0,1,1,1,1,0)

When the number 1 occurs at least 3 times in a row, I'd like to count it as 1. So the required output for the above would be 2.

Upvotes: 2

Views: 81

Answers (3)

Colonel Beauvel
Colonel Beauvel

Reputation: 31181

A solution with stringi:

library(stringi)
stri_count(paste(data, collapse=''), regex='1{3,}')
#[1] 2

stringr does the job but seems to be slower:

library(stringr)
str_count(paste(data, collapse=''), '1{3,}')
#[1] 2

You can also try this base R approach:

length(strsplit(gsub('1{3,}', 'a', paste(data, collapse='')), 'a')[[1]]) - 1
#[1] 2

Upvotes: 2

akrun
akrun

Reputation: 887531

Another option is rleid from data.table

library(data.table)#v1.9.6+
sum(table(rleid(data),data)[,2]>=3)
#[1] 2

Upvotes: 2

Rich Scriven
Rich Scriven

Reputation: 99351

You can use rle()

with(rle(data), sum(lengths >= 3 & values == 1))
# [1] 2

Upvotes: 6

Related Questions