R/tools.R

Defines functions swap_map swap_letter

Documented in swap_letter swap_map

#' Replace a Letter in a Character Vector
#'
#' @param .string character vector
#' @param .target character to replace
#' @param .replacement character to substitute
#'
#' @return the modified string
#'
swap_letter <- function(.string, .target, .replacement) {
    .string[which(.string == .target)] <- .replacement
    .string
}

#' Replace Multiple Letters in a Character Vector
#'
#' @param .string character vector
#' @param .targets characters to replace
#' @param .replacements characters to substitute
#'
#' @return the modified string
#'
swap_map <- function(.string, .targets, .replacements) {
    if (length(.targets) != length(.replacements)) {
        stop("Target and replacement vectors must be of the same length.")
    }
    for (i in seq_along(.targets)) {
        .string <- swap_letter(.string, .targets[i], .replacements[i])
    }
    .string
}

Try the vindecodr package in your browser

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

vindecodr documentation built on Nov. 25, 2020, 5:07 p.m.