diff --git a/NAMESPACE b/NAMESPACE index e46205f..a0e770c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -12,6 +12,7 @@ export(only) export(replace_na_if) export(str_crush) export(str_detect2) +export(str_replace_vec) export(str_to_snake_case) export(summarise2) export(summarize2) diff --git a/R/str-replace_vec.R b/R/str-replace_vec.R new file mode 100644 index 0000000..f178293 --- /dev/null +++ b/R/str-replace_vec.R @@ -0,0 +1,34 @@ +#' String replace multiple strings +#' +#' String replace multiple strings in a vector. +#' +#' `str_replace_vec()` is a vectorized form of [`stringr::str_replace()`]. +#' +#' This is different from passing a named vector to [`stringr::str_replace_all`], +#' which performs multiple replacements but to all pattern matches in a string. +#' +#' @param replace A character vector where the names are the patterns to look +#' for and the values are the replacement values (c(pattern1 = replacement1)) +#' @inherit stringr::str_replace +#' @seealso [`stringr::str_replace()`] and [`stringr::str_replace_all()`] +#' @export +#' @examples +#' fruits <- c("two apples", "nine pears") +#' str_replace_vec(fruits, c("two" = "three", "nine" = "ten")) + +str_replace_vec <- function(string, replace) { + chk_character(string) + chk_character(replace) + chk_named(replace) + + if(!length(replace)) return(string) + + pattern <- names(replace) + + n <- length(replace) + + for(i in seq_len(n)) { + string <- stringr::str_replace(string, pattern = pattern[i], replacement = replace[i]) + } + string +} \ No newline at end of file diff --git a/man/str_replace_vec.Rd b/man/str_replace_vec.Rd new file mode 100644 index 0000000..7e9368b --- /dev/null +++ b/man/str_replace_vec.Rd @@ -0,0 +1,35 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/str-replace_vec.R +\name{str_replace_vec} +\alias{str_replace_vec} +\title{String replace multiple strings} +\usage{ +str_replace_vec(string, replace) +} +\arguments{ +\item{string}{Input vector. Either a character vector, or something +coercible to one.} + +\item{replace}{A character vector where the names are the patterns to look +for and the values are the replacement values (c(pattern1 = replacement1))} +} +\value{ +A character vector the same length as +\code{string}/\code{pattern}/\code{replacement}. +} +\description{ +String replace multiple strings in a vector. +} +\details{ +\code{str_replace_vec()} is a vectorized form of \code{\link[stringr:str_replace]{stringr::str_replace()}}. + +This is different from passing a named vector to \code{\link[stringr:str_replace]{stringr::str_replace_all}}, +which performs multiple replacements but to all pattern matches in a string. +} +\examples{ +fruits <- c("two apples", "nine pears") +str_replace_vec(fruits, c("two" = "three", "nine" = "ten")) +} +\seealso{ +\code{\link[stringr:str_replace]{stringr::str_replace()}} and \code{\link[stringr:str_replace]{stringr::str_replace_all()}} +} diff --git a/tests/testthat/test-str-replace-vec.R b/tests/testthat/test-str-replace-vec.R new file mode 100644 index 0000000..d04a5b2 --- /dev/null +++ b/tests/testthat/test-str-replace-vec.R @@ -0,0 +1,61 @@ +test_that("str_replace_vec works with mutate", { + data <- tibble::tibble( + x = c("one", "two", "three", "four"), + y = c("one apple", "two bananas", "three pear", "four oranges")) + + data <- dplyr::mutate(data, + x2 = str_replace_vec(x, c("one" = "1", + "two" = "2", + "three" = "3", + "four" = "4"))) + + expect_identical(data$x2, c("1", "2", "3", "4")) + expect_identical(data$y, c("one apple", "two bananas", "three pear", "four oranges")) +}) + +test_that("str_replace_vec with regex", { + x <- c("REP_1", "REP1-2", "MR REP 2") + + x2 <- str_replace_vec(x, c("(?