From 890ab846a76e749fedf8177336197737206c1d44 Mon Sep 17 00:00:00 2001 From: Shians Date: Wed, 2 Aug 2017 11:07:23 +1000 Subject: [PATCH 1/6] Added str_remove --- R/remove.r | 22 ++++++++++++++++++++++ tests/testthat/test-remove.r | 6 ++++++ 2 files changed, 28 insertions(+) create mode 100644 R/remove.r create mode 100644 tests/testthat/test-remove.r diff --git a/R/remove.r b/R/remove.r new file mode 100644 index 00000000..79ba8aae --- /dev/null +++ b/R/remove.r @@ -0,0 +1,22 @@ +#' Remove matched patterns in a string. +#' +#' Alias for str_replace(string, pattern, ""). +#' +#' @inheritParams str_detect +#' +#' @return A character vector. +#' @seealso \code{\link{str_replace}} for the underlying implementation. +#' @export +#' @examples +#' fruits <- c("one apple", "two pears", "three bananas") +#' str_remove(fruits, "[aeiou]") +#' str_remove_all(fruits, "[aeiou]") +str_remove <- function(string, pattern) { + str_replace(string, pattern, "") +} + +#' @export +#' @rdname str_replace +str_remove_all <- function(string, pattern) { + str_replace_all(string, pattern, "") +} \ No newline at end of file diff --git a/tests/testthat/test-remove.r b/tests/testthat/test-remove.r new file mode 100644 index 00000000..f86dfb5e --- /dev/null +++ b/tests/testthat/test-remove.r @@ -0,0 +1,6 @@ +context("Removal") + +test_that("basic removal works", { + expect_equal(str_replace_all("abababa", "ba"), "a") + expect_equal(str_replace("abababa", "ba"), "ababa") +}) \ No newline at end of file From bb89cd1bc7fa811aadacaf808640440534486ce2 Mon Sep 17 00:00:00 2001 From: Shians Date: Wed, 2 Aug 2017 11:13:49 +1000 Subject: [PATCH 2/6] Added documentation --- NAMESPACE | 2 ++ man/str_remove.Rd | 41 +++++++++++++++++++++++++++++++++++++++++ man/str_replace.Rd | 7 +++++-- 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 man/str_remove.Rd diff --git a/NAMESPACE b/NAMESPACE index 026354fb..db91106a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -25,6 +25,8 @@ export(str_match) export(str_match_all) export(str_order) export(str_pad) +export(str_remove) +export(str_remove_all) export(str_replace) export(str_replace_all) export(str_replace_na) diff --git a/man/str_remove.Rd b/man/str_remove.Rd new file mode 100644 index 00000000..80093241 --- /dev/null +++ b/man/str_remove.Rd @@ -0,0 +1,41 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/remove.r +\name{str_remove} +\alias{str_remove} +\title{Remove matched patterns in a string.} +\usage{ +str_remove(string, pattern) +} +\arguments{ +\item{string}{Input vector. Either a character vector, or something +coercible to one.} + +\item{pattern}{Pattern to look for. + + The default interpretation is a regular expression, as described + in \link[stringi]{stringi-search-regex}. Control options with + \code{\link{regex}()}. + + Match a fixed string (i.e. by comparing only bytes), using + \code{\link{fixed}(x)}. This is fast, but approximate. Generally, + for matching human text, you'll want \code{\link{coll}(x)} which + respects character matching rules for the specified locale. + + Match character, word, line and sentence boundaries with + \code{\link{boundary}()}. An empty pattern, "", is equivalent to + \code{boundary("character")}.} +} +\value{ +A character vector. +} +\description{ +Alias for str_replace(string, pattern, ""). +} +\examples{ +fruits <- c("one apple", "two pears", "three bananas") +str_remove(fruits, "[aeiou]") +str_remove_all(fruits, "[aeiou]") +} +\seealso{ +\code{\link{str_replace}} for the underlying implementation. +} diff --git a/man/str_replace.Rd b/man/str_replace.Rd index 31337141..1374fede 100644 --- a/man/str_replace.Rd +++ b/man/str_replace.Rd @@ -1,10 +1,13 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/replace.r -\name{str_replace} +% Please edit documentation in R/remove.r, R/replace.r +\name{str_remove_all} +\alias{str_remove_all} \alias{str_replace} \alias{str_replace_all} \title{Replace matched patterns in a string.} \usage{ +str_remove_all(string, pattern) + str_replace(string, pattern, replacement) str_replace_all(string, pattern, replacement) From 96d19cdc16f0d29dee0722ab51275bf33e715f99 Mon Sep 17 00:00:00 2001 From: Shians Date: Wed, 2 Aug 2017 11:14:00 +1000 Subject: [PATCH 3/6] Fixed tests --- tests/testthat/test-remove.r | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test-remove.r b/tests/testthat/test-remove.r index f86dfb5e..9eaadeba 100644 --- a/tests/testthat/test-remove.r +++ b/tests/testthat/test-remove.r @@ -1,6 +1,6 @@ context("Removal") test_that("basic removal works", { - expect_equal(str_replace_all("abababa", "ba"), "a") - expect_equal(str_replace("abababa", "ba"), "ababa") -}) \ No newline at end of file + expect_equal(str_remove_all("abababa", "ba"), "a") + expect_equal(str_remove("abababa", "ba"), "ababa") +}) From d50ddf021958bba24bce6a4477068ef8526108e3 Mon Sep 17 00:00:00 2001 From: Shians Date: Wed, 2 Aug 2017 11:15:17 +1000 Subject: [PATCH 4/6] Fixed documentation --- R/remove.r | 2 +- man/str_remove.Rd | 3 +++ man/str_replace.Rd | 7 ++----- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/R/remove.r b/R/remove.r index 79ba8aae..9c7543e4 100644 --- a/R/remove.r +++ b/R/remove.r @@ -16,7 +16,7 @@ str_remove <- function(string, pattern) { } #' @export -#' @rdname str_replace +#' @rdname str_remove str_remove_all <- function(string, pattern) { str_replace_all(string, pattern, "") } \ No newline at end of file diff --git a/man/str_remove.Rd b/man/str_remove.Rd index 80093241..261c8113 100644 --- a/man/str_remove.Rd +++ b/man/str_remove.Rd @@ -2,9 +2,12 @@ % Please edit documentation in R/remove.r \name{str_remove} \alias{str_remove} +\alias{str_remove_all} \title{Remove matched patterns in a string.} \usage{ str_remove(string, pattern) + +str_remove_all(string, pattern) } \arguments{ \item{string}{Input vector. Either a character vector, or something diff --git a/man/str_replace.Rd b/man/str_replace.Rd index 1374fede..31337141 100644 --- a/man/str_replace.Rd +++ b/man/str_replace.Rd @@ -1,13 +1,10 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/remove.r, R/replace.r -\name{str_remove_all} -\alias{str_remove_all} +% Please edit documentation in R/replace.r +\name{str_replace} \alias{str_replace} \alias{str_replace_all} \title{Replace matched patterns in a string.} \usage{ -str_remove_all(string, pattern) - str_replace(string, pattern, replacement) str_replace_all(string, pattern, replacement) From 17401f32ad6ff64299749de3b3cf8b0f63f07d57 Mon Sep 17 00:00:00 2001 From: Shians Date: Wed, 2 Aug 2017 11:39:03 +1000 Subject: [PATCH 5/6] Changed tabs to spaces --- R/remove.r | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/remove.r b/R/remove.r index 9c7543e4..8fdc5a38 100644 --- a/R/remove.r +++ b/R/remove.r @@ -12,11 +12,11 @@ #' str_remove(fruits, "[aeiou]") #' str_remove_all(fruits, "[aeiou]") str_remove <- function(string, pattern) { - str_replace(string, pattern, "") + str_replace(string, pattern, "") } #' @export #' @rdname str_remove str_remove_all <- function(string, pattern) { - str_replace_all(string, pattern, "") + str_replace_all(string, pattern, "") } \ No newline at end of file From 21f2c00a9239e7d5e1632b4df843f266bbbec841 Mon Sep 17 00:00:00 2001 From: Shians Date: Wed, 15 Nov 2017 10:14:21 +1100 Subject: [PATCH 6/6] Updated NEWS --- NEWS.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NEWS.md b/NEWS.md index 98317030..efa4af9f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -12,6 +12,10 @@ * `str_trunc()` now preserves NAs (@ClaytonJY, #162) +* New `str_remove()` and `str_remove_all()` functions. These wrap + `str_replace()` and `str_replace_all()` to remove patterns from strings. + (@Shians, #178) + # stringr 1.2.0 ## API changes