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 2
r <- rle(c(1, pmin(diff(x), 2)))

# paste selected x values with appropriate separator
res <- paste0(x[c(1, cumsum(r$lengths))], c("-", ",")[r$values], collapse="")

# drop final character, which is a separator
res <- 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"