R/recycle.R

#' Variable recycler
#' 
#' Recycle the elements in \code{x} to make a vector that has length \code{N}.
#' 
#' Typical usages are: \preformatted{ recycle(1:8, 17) recycle(c("red", "blue",
#' "green"), 7) } If \code{length(x) == 1} then this is equivalent to
#' \code{rep(x)}. \code{N} does not have to be an even number of multiples of
#' \code{length(x)}.
#' 
#' @param x A vector of length >= 1
#' @param N The desired length of the result. Should be >= 2.
#' @return A vector of length N, with elements made up by repeating x as many
#'   times as necessary.
#' @author Mark Cowley, 9 Nov 2005
#' @seealso \code{\link{rep}}
#' @keywords manip
#' @examples
#' 
#' recycle(1:8, 17)
#' recycle(c("red", "blue", "green"), 7)
#' @export
recycle <- function(x, N) {
    if( length(x) < N )
        return( rep(x, N)[1:N] )
    else
        return( x[1:N] )
}
drmjc/mjcbase documentation built on May 15, 2019, 2:27 p.m.