Sequential citation numbering in R: separate numbers by hyphen, if sequential - add comma if not Sequential citation numbering in R: separate numbers by hyphen, if sequential - add comma if not r r

Sequential citation numbering in R: separate numbers by hyphen, if sequential - add comma if not


You can do this easily via base R using tapply,

paste(tapply(x, cumsum(c(1, diff(x) != 1)), function(i)     ifelse(length(i) > 2, paste0(head(i, 1), '-', tail(i, 1)),                             paste(i, collapse = ','))), collapse = ',')[1] "1-3,5,6,8-11,13"


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"