Anuj Amin
Anuj Amin

Reputation: 77

how to find the total number of consecutive digits in a sequence of numbers

I've got a list of numbers [2554, 201, 452, 3111, 4133, 2210, 1235, 430, 4210, 11, 513, 1305, 2550, 5312, 512, 3535, 1014, 4013, 234, 4112, 13, 2433, 5233, 4500]. I'm trying to found out have many of these numbers contain consecutive digits. For example 2554 has consecutive digits (5) but 513 and 3535 do not. I know the total amount of numbers with consecutive digits is 10 out of 24.

#my code
count = 0
z = c(2554, 201, 452, 3111, 4133, 2210, 1235, 430, 4210, 11, 513, 1305, 2550, 5312, 512, 3535, 1014, 4013, 234, 4112, 13, 2433, 5233, 4500)
for(i in 1:24){
  if((grep(pattern = "\\d{2}", x = z[i])) == 1){
    count = count + 1
  }
}

I'm trying to use grep but it's not working, is there anything I'm doing wrong?

Upvotes: 3

Views: 302

Answers (3)

Retired Data Munger
Retired Data Munger

Reputation: 1445

You can use 'rle' to finds 'runs' of characters

library(tidyverse)
z = c(2554, 201, 452, 3111, 4133, 2210, 
      1235, 430, 4210, 11, 513, 1305, 
      2550, 5312, 512, 3535, 1014, 
      4013, 234, 4112, 13, 2433, 5233, 4500)

# split out to separate character each digit and then use 'rle' to
# look for 'runs' of the characters
as_char <- map_dfr(z, ~{
  str <- strsplit(as.character(.x), '')[[1]]
  rle_str <- rle(str)
  # find which have runs > 1
  indx <- which(rle_str$lengths > 1)
  tibble(input = .x, count = rle_str$lengths[indx], value = rle_str$values[indx])
})
as_char

> as_char
# A tibble: 10 x 3
input count value
<dbl> <int> <chr>
  1  2554     2 5    
2  3111     3 1    
3  4133     2 3    
4  2210     2 2    
5    11     2 1    
6  2550     2 5    
7  4112     2 1    
8  2433     2 3    
9  5233     2 3    
10  4500     2 0    

Upvotes: 0

akrun
akrun

Reputation: 887601

We can use str_detect

library(stringr)
sum(str_detect(z, '(\\d)\\1'))
#[1] 10

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 389175

You don't need for loop in grep/grepl since they are vectorized.

To find consecutive digits, you can use the backreference of first digit of capture group in grepl.

sum(grepl('(\\d)\\1', z))
#[1] 10

Or similarly with grep

length(grep('(\\d)\\1', z))

To know which numbers have consecutive digits, you can use value = TRUE in grep.

grep('(\\d)\\1', z, value = TRUE)
#[1] "2554" "3111" "4133" "2210" "11"   "2550" "4112" "2433" "5233" "4500"

Upvotes: 5

Related Questions