Count Consecutive Numbers in a Vector

Count the number of consecutive occurrences in a vector

We can use rle and diff functions :

a=rle(diff(vec))
sum(a$values==1)

Count of consecutive numbers in a vector

Something like this?

size_t conseccount(const vector<int> &v)
{
size_t s = v.size();
size_t ret = 0;
bool seqstart = true;
if(s < 2) return 0;
for(size_t i = 1; i < s; i++)
{
if(v[i - 1] + 1 == v[i])
{
if(seqstart) ret++;
seqstart = false;
ret++;
}
else seqstart = true;
}
return ret;
}

Function to count of consecutive digits in a string vector

Try rle + strsplit if you are working with base R

f <- function(s) {
with(
rle(unlist(strsplit(s, ""))),
any(as.numeric(values) <= lengths & lengths > 1)
)
}

and you will see

> f("555123")
[1] FALSE

> f("57333")
[1] TRUE

Count the number of consecutive pairs in a vector

All consecutive pairs can be represented by two parallel vectors, omitting the last or the first observation

x <- V[-length(V)]
y <- V[-1]

and then cross-tabulating these

> xtabs(~ x + y)
y
x -1 1
-1 7 1
1 0 1

or in slightly different form

> as.data.frame(xtabs(~x+y))
x y Freq
1 -1 -1 7
2 1 -1 0
3 -1 1 1
4 1 1 1

Count consecutive elements in a same length vector

Another option using rle and rep

with(rle(a), rep(lengths, times = lengths))
# [1] 2 2 3 3 3 4 4 4 4 2 2 3 3 3

data

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

Get a vector with the count of consecutive specific values in R

Edit: Did not see Roman's identical solution when posting.

We would like something like:

tmp <- rle(with(df, !Movement & Booked))
tmp$lengths[tmp$values]

The indexing by tmp$values ensures you only get the rows corresponding to the pattern you've specified.

Hope this helps!

Count largest number of consecutive zeros in a vector using a vectorized function

You can use rle and max to count largest number of consecutive zeros.

x <- rle(a==0)
max(x$lengths[x$values == TRUE])
#[1] 2

How to count consecutive numbers in an array?

array.each_with_index.
chunk(&:first).
select { |_,a| a.size > 3 }.
map { |n,a| { starting_index: a.first.last, value: n, length: a.size } }
#=> [{:starting_index=> 1, :value=>2, :length=>4},
# {:starting_index=>10, :value=>3, :length=>4},
# {:starting_index=>14, :value=>2, :length=>7}]

The steps are as follows.

e = array.each_with_index.chunk(&:first)
#=> #<Enumerator: #<Enumerator::Generator:0x00005b1944253c18>:each>

We can convert this enumerator to an array to view the elements it will generate and pass to its block.

e.to_a
#=> [[1, [[1, 0]]],
# [2, [[2, 1], [2, 2], [2, 3], [2, 4]]],
# [5, [[5, 5], [5, 6]]],
# [1, [[1, 7], [1, 8], [1, 9]]],
# [3, [[3, 10], [3, 11], [3, 12], [3, 13]]],
# [2, [[2, 14], [2, 15], [2, 16], [2, 17], [2, 18], [2, 19], [2, 20]]]]

Continuing,

c = e.select { |_,a| a.size > 3 }
#=> [[2, [[2, 1], [2, 2], [2, 3], [2, 4]]],
# [3, [[3, 10], [3, 11], [3, 12], [3, 13]]],
# [2, [[2, 14], [2, 15], [2, 16], [2, 17], [2, 18], [2, 19], [2, 20]]]]
c.map { |n,a| { starting_index: a.first.last, value: n, length: a.size } }
#=> [{:starting_index=> 1, :value=>2, :length=>4},
# {:starting_index=>10, :value=>3, :length=>4},
# {:starting_index=>14, :value=>2, :length=>7}]

This is another way.

array.each_with_index.with_object([]) do |(n,i),arr|
if arr.any? && arr.last[:value] == n
arr.last[:length] += 1
else
arr << { starting_index: i, value: n, length: 1 }
end
end.select { |h| h[:length] > 3 }
#=> [{:starting_index=> 1, :value=>2, :length=>4},
# {:starting_index=>10, :value=>3, :length=>4},
# {:starting_index=>14, :value=>2, :length=>7}]


Related Topics



Leave a reply



Submit