Sequential citation numbering in R: separate numbers by hyphen, if sequential - add comma if not
This works for your example and should be fairly general.
# get run lengths of differences, with max value of 2r <- rle(c(1, pmin(diff(x), 2)))# paste selected x values with appropriate separatorres <- paste0(x[c(1, cumsum(r$lengths))], c("-", ",")[r$values], collapse="")# drop final character, which is a separatorres <- substr(res, 1, nchar(res)-1)
This returns
res[1] "1-3,5-6,8-11,13"
There is, of course, the seqToHumanReadable
function from the "R.utils" package.
library(R.utils)seqToHumanReadable(x)# [1] "1-3, 5, 6, 8-11, 13"seqToHumanReadable(x, tau = 1) ## If you want 5-6 and not 5, 6# [1] "1-3, 5-6, 8-11, 13"
The appearance of the result can also be controlled:
seqToHumanReadable(x, delimiter = "...", collapse = " | ")# [1] "1...3 | 5 | 6 | 8...11 | 13"