How to find the starting position for consecutive values given the length of consecutive values in an R vector?

R ProgrammingServer Side ProgrammingProgramming

To find the starting position for consecutive values given the length of consecutive values in an R vector, we can follow the below steps −

  • First of all, create a vector with duplicated values.
  • Find the run length.
  • Create a vector with FALSE of vector size.
  • Find the starting position by setting running length of consecutive values to TRUE for given length.

Create the vector

Let’s create a vector having many duplciates so that we can get same consecutive values −

 Live Demo

x<-sample(0:2,250,replace=TRUE)
x

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

[1] 0 2 2 0 2 1 2 2 2 1 1 1 0 1 0 2 0 2 2 1 2 2 1 0 2 1 2 0 2 0 0 2 2 0 0 2 2
[38] 0 2 0 1 2 2 1 2 2 1 0 2 2 2 0 1 2 2 2 2 2 1 2 2 2 0 0 2 0 0 0 2 1 2 1 2 2
[75] 2 1 0 1 0 1 2 0 1 0 2 0 1 2 2 2 1 0 2 1 2 2 0 2 0 0 1 0 1 1 0 0 1 2 1 0 1
[112] 0 1 1 0 0 0 2 2 0 1 1 0 1 0 0 2 1 2 2 1 0 0 1 0 0 0 2 0 2 2 0 0 2 1 1 0 0
[149] 2 0 2 0 1 1 1 1 0 2 1 0 0 2 2 1 1 0 1 2 0 0 0 2 2 1 2 0 2 0 1 1 2 0 2 0 2
[186] 0 0 1 0 0 0 1 2 0 0 2 1 1 2 1 0 1 0 1 1 2 1 0 1 0 0 1 1 0 2 0 1 1 1 2 0 0
[223] 1 0 2 1 2 1 1 2 2 1 2 1 0 1 2 2 0 0 2 1 2 2 2 2 2 1 2 0

Find the run length

Use rle function to find the running length of values in the vector −

 Live Demo

x<-sample(0:2,250,replace=TRUE)
Run_tab<-rle(x)
Run_tab

Output

Run Length Encoding
lengths: int [1:175] 1 2 1 1 1 3 3 1 1 1 ...
values : int [1:175] 0 2 0 2 1 2 1 0 1 0 ...

Create a vector with FALSE values

Use rep function to create a vector of size equal to the above vector with each value as FALSE −

 Live Demo

x<-sample(0:2,250,replace=TRUE)
Run_tab<-rle(x)
Length_x<-rep(FALSE,length(x))
Length_x

Output

[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[49] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[61] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[73] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[97] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[109] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[121] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[133] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[145] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[157] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[169] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[181] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[193] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[205] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[217] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[229] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[241] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

Find the starting position with TRUE

Use sequence function to find the starting position by setting running length 2 or more to TRUE −

 Live Demo

x<-sample(0:2,250,replace=TRUE)
Run_tab<-rle(x)
Length_x<-rep(FALSE,length(x))
Length_x[sequence(Run_tab$lengths)==1]<-Run_tab$lengths>= 2 &
Run_tab$values==2
Length_x

Output

[1] FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE
[25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE
[37] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE
[49] TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE
[61] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[73] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[85] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
[97] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[109] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
[121] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
[133] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
[145] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[157] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
[169] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[181] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[193] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[205] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[217] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[229] FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
[241] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
raja
Published on 06-Aug-2021 13:31:26
Advertisements