File: toRangeStr.R

package info (click to toggle)
r-cran-bbmisc 1.13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,256 kB
  • sloc: ansic: 176; sh: 9; makefile: 5
file content (37 lines) | stat: -rw-r--r-- 1,230 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#' @title Convert a numerical vector into a range string
#'
#' @description
#' Converts a vector of integers into a compact range string representation.
#'
#' @param x [\code{integer}]\cr
#'   Vector to convert into a range string.
#' @param range.sep [\code{character(1)}]\cr
#'   Separator between the first and last element of a range of consecutive
#'   elements in \code{x}.
#'   Default is \dQuote{ - }.
#' @param block.sep [\code{character(1)}]\cr
#'   Separator between non consecutive elements of \code{x} or ranges.
#'   Default is \dQuote{, }.
#' @return [\code{character(1)}]
#' @examples
#' x = sample(1:10, 7)
#' toRangeStr(x)
#' @export
toRangeStr = function(x, range.sep = " - ", block.sep = ", ") {
  if (testIntegerish(x))
    x = as.integer(x)
  else
    assertNumeric(x, any.missing = FALSE)
  assertString(range.sep)
  assertString(block.sep)

  findRange = function(x) seq_len(max(which(x == x[1L] + 0:(length(x)-1L))))
  x = sort(unique(x))
  x = unname(split(x, c(0L, cumsum(diff(x) > 1L))))
  combine = function(x)
    if (length(x) == 1L)
      as.character(x)
    else
      sprintf("%i%s%i", x[1L], range.sep, x[length(x)])
  collapse(vapply(x, combine, character(1L), USE.NAMES = FALSE), block.sep)
}