R/vec2range.R

Defines functions vec2range

Documented in vec2range

#' Convert a vector of integers in to a matrix of ranges
#'
#' A function which converts a one-dimensional vector into a 2-column matrix of pairwise ranges that represent the range of numbers between subsequent values
#'
#' @param vec A vector of integers
#' @param with.max (optional) A number to use as the end point of the last range
#'
#' @return matrix whose rows contain start-end pairs from original vector
#' @importFrom utils head tail
#' @export
vec2range <- function(vec, with.max=NULL){
	stopifnot(is.numeric(vec))
	out = cbind(
		head(vec,-1),
		(tail(vec,-1)-1)
	)
	if(!is.null(with.max)){
		out = rbind(out, c( tail(vec,1), with.max) )
	}
	out
}
stackcon/rngt documentation built on June 17, 2022, 5:29 p.m.