I am looking for an efficient way to simplify a vector of integers as a summary string, in order to format it to fit in a table cell.
For example:
c(1, 2, 3, 4, 6, 8, 9, 10)
should produce
"1-4, 6, 8-10"
This becomes especially useful in cases where printing all elements in the vector would quickly make the table unreadable.
e.g.
c(1:50, 53, 89:120)
should produce
"1-50, 53, 89-120"
You want to group the elements into blocks of consecutive integers.
diffcan tell you if two consecutive elements are in the same block,cumsumcan number the blocksand
tapplycan extract the first and last element of each block.