0

I have the following vector:

essaie <- "23690,55050,127789,64253,130127,63093,63093,63093,108374,63093,63093"
essaie_z <- c(as.numeric(unlist(strsplit(essaie, ","))))

I want to know which number repeats itself consecutively the most and how many time it does. Basically, I want to know:

Number that repeats itself consecutively the most: 63093 How many times: 3

How can I do that in r?

thanks,

s_baldur
  • 29,441
  • 4
  • 36
  • 69
lmcjfal
  • 85
  • 5
  • What do you want to do if there are two numbers that have the same max? – Andrew Nov 07 '19 at 14:24
  • @sindri_baldur bad duplicate suggestion. This has the **consecutive** requirement, which makes it substantially different. (There probably is a good dupe out there, but that one isn't it.) – Gregor Thomas Nov 07 '19 at 14:37

2 Answers2

4

You can use the function rle:

runs <- rle(essaie_z)
max(runs$lengths)
# [1] 3
runs$values[which.max(runs$lengths)]
# [1] 63093

Explanation rle stands for run length encoding. It reports the runs in the data as well as the lengths of these runs, returning the result as a list:

> rle(essaie_z)
Run Length Encoding
  lengths: int [1:8] 1 1 1 1 1 3 1 2
  values : num [1:8] 23690 55050 127789 64253 130127 63093 108374 63093

What you are asking for is the value which corresponds to the longest run.

John Coleman
  • 51,337
  • 7
  • 54
  • 119
  • Thanks a lot. Your solution works on a vector. I tried it on a tibble but I couldn't get it to work. I posted a new question on how to get a run length in a tibble: https://stackoverflow.com/questions/58755481/how-to-find-the-longest-duplicate-sequence-in-a-tibble-column – lmcjfal Nov 07 '19 at 19:11
1

Here is an alternative,

i1 <- strsplit(essaie, ',')[[1]]
l1 <- split(i1, cumsum(c(TRUE, diff(as.numeric(i1)) != 0)))
l1[which.max(lengths(l1))]
#$`6`
#[1] "63093" "63093" "63093"

And to get the length,

length(l1[which.max(lengths(l1))][[1]])
#[1] 3

Not sure how you want your output.

 paste0('value: ', unique(l1[which.max(lengths(l1))][[1]]), ', Repetition: ', length(l1[which.max(lengths(l1))][[1]]))
#[1] "value: 63093, Repetition: 3"
Sotos
  • 51,121
  • 6
  • 32
  • 66