4

This is a duplicate question to this, except for R rather than Python.

I'd like to identify groups of contiguous (some people call them continuous) integers in a list, where duplicate entries are treated as existing within the same range. Therefore:

myfunc(c(2, 3, 4, 4, 5, 12, 13, 14, 15, 16, 17, 17, 20))

returns:

min  max
2    5
12   17
20   20

Although any output format would be fine. My current brute-force, for-loop method is pretty slow.

(Apologies if I could have easily re-interpreted the Python answer and I'm being stupid!)

Community
  • 1
  • 1
canary_in_the_data_mine
  • 2,193
  • 2
  • 24
  • 28
  • I hadn't found that question when I was searching! Thanks. I'm trying to figure out how to format the output from that other answer in the format requested above. If I can figure it out from the other answer, I'll close this question. Thanks. – canary_in_the_data_mine May 01 '13 at 19:34

1 Answers1

7

Just use diff:

x = c(2, 3, 4, 4, 5, 12, 13, 14, 15, 16, 17, 17, 20)

start = c(1, which(diff(x) != 1 & diff(x) != 0) + 1)
end = c(start - 1, length(x))

x[start]
# 2 12 20
x[end]
# 5 17 20
eddi
  • 49,088
  • 6
  • 104
  • 155
  • This comes very close! Forgot to properly describe my question in the initial ask: it'd be great if the function treated duplicate integers as existing within the same range. Question edited now to reflect this. Thanks. – canary_in_the_data_mine May 01 '13 at 19:53
  • @canary_in_the_data_mine that's an easy fix - see edit – eddi May 01 '13 at 19:54