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 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
How Do I Find Consecutive Values
In case 1 1 1 1
means for you that 1 1 1
actually appears twice, you could do:
sum(stats::filter(x, c(1, 1, 1), circular = TRUE, sides = 2) == 3)
[1] 7
Count the number of consecutive occurrences in a vector
We can use rle
and diff
functions :
a=rle(diff(vec))
sum(a$values==1)
Create a list of vectors of consecutive values from a vector
An option is to split
by creating a grouping variable created by checking the diff
erence of adjacent elements
split(vec, cumsum(c(TRUE, diff(vec) != 1)))
#$`1`
#[1] 1 2
#$`2`
#[1] 5
#$`3`
#[1] 7 8 9
#$`4`
#[1] 11 12 13
#$`5`
#[1] 15
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
Related Topics
R - Faster Way to Calculate Rolling Statistics Over a Variable Interval
Two Horizontal Bar Charts with Shared Axis in Ggplot2 (Similar to Population Pyramid)
Implementation of Standard Recycling Rules
Can't Run Rcpp Function in Foreach - "Null Value Passed as Symbol Address"
How to Change Angle of Line in Customized Legend in Ggplot2
Ggplot2 - Using Two Different Color Scales for Overlayed Plots
Showing Equation of Nls Model with Ggpmisc
How to Stack Only Some Columns in a Data Frame
How to Convert Month-Year String to Date in R
Importing Excel File Using Url Using Read.Xls
Fitting a Curve to Specific Data
Reading Big Data with Fixed Width
Element-Wise Concatenation of String Vectors
Identifying the Outliers in a Data Set in R
List Members Can Be Accessed with Partial Name? Is This a Feature
R Remove Last Word from String
Select Rows of a Data.Frame That Contain Only Numbers in a Certain Column