R/metdat.R

Defines functions metdat

Documented in metdat

#' Extracts (and merges) chunks of characters.
#'
#' @param x A character vector.
#' @param i Index specifying which chunks of characters 
#' will be extracted (and merged).
#' @param sep Chunks separator character. Defaults to "-".
#' @param collapse New chunks separator character.
#' Default to 'sep'.
#' @returns A character vector with the extracted (and merged) chunks of
#' characters.
#' @export
#' @examples
#' x <- "this is one chunk of characters & this is another one"
#' metdat(x, 1, " & ")
#' metdat(x, 2, " & ")
#' metdat(x, c(1, 2), " & ")
#' metdat(x, c(1, 2), " & ", " and ")
metdat <- function(x, i, sep = "-", collapse = sep) {
  unlist(lapply(strsplit(x, sep, T), function(x) {
    paste(x[i],
      collapse = collapse
    )
  }))
}

Try the MOSS package in your browser

Any scripts or data that you put into this service are public.

MOSS documentation built on March 26, 2022, 1:10 a.m.