How to Check If a Vector Contains N Consecutive Numbers

How to check if a vector contains n consecutive numbers

Using diff and rle, something like this should work:

result <- rle(diff(numbers))
any(result$lengths>=2 & result$values==1)
# [1] TRUE

In response to the comments below, my previous answer was specifically only testing for runs of length==3 excluding longer lengths. Changing the == to >= fixes this. It also works for runs involving negative numbers:

> numbers4 <- c(-2, -1, 0, 5, 7, 8)
> result <- rle(diff(numbers4))
> any(result$lengths>=2 & result$values==1)
[1] TRUE

Determine sequence of consecutive numbers in R

Is this what you're after?

split(y, cumsum(c(0, diff(y) > 4)));
#$`0`
#[1] 4 8 12 16
#
#$`1`
#[1] 24
#
#$`2`
#[1] 31 33
#
#$`3`
#[1] 39
#
#$`4`
#[1] 64 68 72 76 80

I don't see 24 in your list; is that a mistake?

If you want to exclude list entries with only one number, you can do everything in one line:

Filter(length, lapply(split(y, cumsum(c(0, diff(y) > 4))), function(x) x[length(x) > 1]));
#$`0`
#[1] 4 8 12 16
#
#$`2`
#[1] 31 33
#
#$`4`
#[1] 64 68 72 76 80

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

Find consecutive values in vector in R

Just use split in conjunction with diff:

> split(dat, cumsum(c(1, diff(dat) != 1)))
$`1`
[1] 1 2 3 4 5

$`2`
[1] 19 20 21

$`3`
[1] 56

$`4`
[1] 80 81

$`5`
[1] 92

Not exactly what you asked for, but the "R.utils" package has a couple of related fun functions:

library(R.utils)
seqToIntervals(dat)
# from to
# [1,] 1 5
# [2,] 19 21
# [3,] 56 56
# [4,] 80 81
# [5,] 92 92
seqToHumanReadable(dat)
# [1] "1-5, 19-21, 56, 80-81, 92"

How to efficiently check if a list of consecutive numbers is missing any elements

Since you know you are expecting a sequential array, I don't know why it needs to be more complicated than a loop through numbers arr[0] through arr[end] while keeping a counter to know where you are in the array. This will run at O(n), but I don't think you can improve on that — you need to look at every element at least once in the worst case.

var arr = ["s00","s01","s02","s03","s04","s05","s07","s08","s09","s10","s11","s12","s13","s14","s17","s19","s20","s21","s22","s24","s25","s26","s27","s28","s30","s32","s33","s34","s36","s38","s39","s41","s43","s44","s45","s46","s47","s48","s49","s50","s51","s52","s53","s54","s55","s56","s58","s60","s61","s62","s63","s64","s65","s67","s69","s70"];
let first = parseInt(arr[0].substring(1))let last = parseInt(arr[arr.length-1].substring(1))let count = 0for (let i = first; i< last; i++) { if (parseInt(arr[count].substring(1)) == i) {count++; continue} else console.log(`seem to be missing ${'s'+i.toString().padStart(2,'0')} between: ${arr[count-1]} and ${arr[count]}` )}

Determine if there are x consecutive duplicates in a vector in R

p<-c(0,0,1,1,1,3,2,3,2,2,2,2)

find.dup <- function(x, n) {
consec <- 1
for(i in 2:length(x)) {
if(x[i] == x[i-1]) {
consec <- consec + 1
} else {
consec <- 1
}
if(consec == n)
return(TRUE) # or you could return x[i]
}
return(FALSE)
}

find.dup(p,3)
# [1] TRUE

find.dup(p,4)
# [1] TRUE

find.dup(p,5)
# [1] FALSE

Find a number in a vector with consecutive numbers

  1. Header import should be #include <iostream>
  2. You need using namespace std after the header, or use std::cout and std::cin
  3. while expression should be while((li<=ls)&&(f==0))- it needs to be one expression without a semi-colon after it. Having while(expr); creates a loop that does nothing - ; counts as an empty statement

Dash between consecutive numbers

In base R using tapply :

data = c(18,20:25,28:30)
result <- unlist(tapply(data, cumsum(c(FALSE, diff(data) > 1)), function(x)
c('-', x)), use.names = FALSE)[-1]

#[1] "18" "-" "20" "21" "22" "23" "24" "25" "-" "28" "29" "30"

In every group of consecutive numbers prepend the sequence with "-".

Group integer vector into consecutive runs

Here's a brief answer using aggregate....

runs <- cumsum( c(0, diff(my.data$V2) > 1) )
aggregate(V2 ~ runs + V1, my.data, range)[,-1]

V1 V2.1 V2.2
1 1 2 5
2 1 7 11
3 1 13 13
4 2 4 9
5 2 11 13
6 3 1 6
7 3 101 105

Convert a vector of a numbers to consecutive integers in R

One option could be:

cumsum(c(0, diff(x)) != 0) + 1

[1] 1 1 2 2 2 3 3 3 3 4 4 5 6 6 7 7


Related Topics



Leave a reply



Submit