Skip to content

Commit d6ef15d

Browse files
yutannihilationhadley
authored andcommitted
str_replace should ignore NA when using function as replacement (#164)
* enable to set omit_na=TRUE on stri_sub<- * update the version of stringi to 1.1.6 * improve description about omit_na
1 parent 72e244c commit d6ef15d

File tree

7 files changed

+46
-6
lines changed

7 files changed

+46
-6
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Depends:
1919
Imports:
2020
glue,
2121
magrittr,
22-
stringi (>= 0.4.1)
22+
stringi (>= 1.1.6)
2323
Suggests:
2424
covr,
2525
htmltools,

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
and also converts multiple space (or space-like characters) to a single
1818
space within strings (@stephlocke, #197).
1919

20+
* `str_sub()` gains `omit_na` argument for ignoring `NA`. Accordingly,
21+
`str_replace()` now ignores `NA`s and keeps the original strings.
22+
(@yutannihilation, #164)
23+
2024
## Bug fixes and minor improvements
2125

2226
* `str_trunc()` now preserves NAs (@ClaytonJY, #162)

R/replace.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ str_replace_na <- function(string, replacement = "NA") {
159159

160160
str_transform <- function(string, pattern, replacement) {
161161
loc <- str_locate(string, pattern)
162-
str_sub(string, loc) <- replacement(str_sub(string, loc))
162+
str_sub(string, loc, omit_na = TRUE) <- replacement(str_sub(string, loc))
163163
string
164164
}
165165
str_transform_all <- function(string, pattern, replacement) {

R/sub.r

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#' matrix to `start`.
1616
#'
1717
#' Negative values count backwards from the last character.
18+
#' @param omit_na Single logical value. If `TRUE`, missing values in any of the
19+
#' arguments provided will result in an unchanged input.
1820
#' @param value replacement string
1921
#' @return A character vector of substring from `start` to `end`
2022
#' (inclusive). Will be length of longest input argument.
@@ -50,6 +52,15 @@
5052
#' str_sub(x, -1, -1) <- "K"; x
5153
#' str_sub(x, -2, -2) <- "GHIJ"; x
5254
#' str_sub(x, 2, -2) <- ""; x
55+
#'
56+
#' # If you want to keep the original if some argument is NA,
57+
#' # use omit_na = TRUE
58+
#' x1 <- x2 <- x3 <- x4 <- "AAA"
59+
#' str_sub(x1, 1, NA) <- "B"
60+
#' str_sub(x2, 1, 2) <- NA
61+
#' str_sub(x3, 1, NA, omit_na = TRUE) <- "B"
62+
#' str_sub(x4, 1, 2, omit_na = TRUE) <- NA
63+
#' x1; x2; x3; x4
5364
str_sub <- function(string, start = 1L, end = -1L) {
5465
if (is.matrix(start)) {
5566
stri_sub(string, from = start)
@@ -61,11 +72,11 @@ str_sub <- function(string, start = 1L, end = -1L) {
6172

6273
#' @export
6374
#' @rdname str_sub
64-
"str_sub<-" <- function(string, start = 1L, end = -1L, value) {
75+
"str_sub<-" <- function(string, start = 1L, end = -1L, omit_na = FALSE, value) {
6576
if (is.matrix(start)) {
66-
stri_sub(string, from = start) <- value
77+
stri_sub(string, from = start, omit_na = omit_na) <- value
6778
} else {
68-
stri_sub(string, from = start, to = end) <- value
79+
stri_sub(string, from = start, to = end, omit_na = omit_na) <- value
6980
}
7081
string
7182
}

man/str_sub.Rd

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-replace.r

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ test_that("replacement can be different length", {
6969
expect_equal(str_replace_all("abc", "a|c", double), "aabcc")
7070
})
7171

72+
test_that("replacement with NA works", {
73+
expect_equal(str_replace("abc", "z", toupper), "abc")
74+
})
75+
7276
# fix_replacement ---------------------------------------------------------
7377

7478
test_that("$ are escaped", {

tests/testthat/test-sub.r

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ test_that("replacement works", {
6969

7070
str_sub(x, 2, -2) <- ""
7171
expect_equal(x, "AH")
72+
})
7273

74+
test_that("replacement with NA works", {
75+
x <- "BBCDEF"
76+
str_sub(x, NA) <- "A"
77+
expect_equal(x, NA_character_)
7378

79+
x <- "BBCDEF"
80+
str_sub(x, NA, omit_na = TRUE) <- "A"
81+
str_sub(x, 1, 1, omit_na = TRUE) <- NA
82+
expect_equal(x, "BBCDEF")
7483
})

0 commit comments

Comments
 (0)