R/repeating_functions.R

#' Replicate element and output a list
#'
#' @param x     element to be repeated.
#' @param times the number of times to repeat \code{x}.
#'
#' @examples
#'
#' rep_list(1:10, 5)
#' rep_list("hello world!", 3)
#' rep_list(list(list(1:5, NA), "a"), 3)
#'
#' @export

rep_list <- function(x, times) {
  if (length(times) > 1L) {
    times <- times[1L]
    warning("times has length > 1 and only the first element will be used")
  }
  if (times < 1L)
    stop("times needs to be > 0")
  lapply(1:times, function(i) x)
}
twolodzko/lolplyr documentation built on May 14, 2019, 8:22 a.m.