R/name_vec.R

Defines functions name_vec

Documented in name_vec

#' @title Create Named Vector
#'
#' @description Create a named vector in a single line without either manually defining names at the outset (e.g., `c("name_1" = 1, "name_2" = 2, ...`) or spending a second line to assign names to an existing vector (e.g., `names(vec) <- c("name_1", "name_2", ...)`). Useful in cases where you need a named vector within a pipe and don't want to break into two pipes just to define a named vector (see `tidyr::separate_wider_position`)
#' 
#' @param content (vector) content of vector
#' @param name (vector) names to assign to vector (must be in same order)
#' 
#' @return (named vector) vector with contents from the `content` argument and names from the `name` argument
#' 
#' @export
#' 
#' @examples
#' # Create a named vector
#' name_vec(content = 1:10, name = paste0("text_", 1:10))
#' 
name_vec <- function(content = NULL, name = NULL){
  
  # Error out either is not provided
  if(is.null(content) == TRUE | is.null(name) == TRUE)
    stop("Both arguments must be specified")
  
  # Error out if content & name are not the same length
  if(length(x = content) != length(x = name))
    stop("Vector content and names must be the same length")
  
  # Construct the named vector
  names(content) <- name
  
  # Return that
  return(content) }

Try the supportR package in your browser

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

supportR documentation built on June 22, 2024, 10:17 a.m.