-
Notifications
You must be signed in to change notification settings - Fork 195
str_replace should ignore NA when using function as replacement #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
3b87fc4
ee448fe
d0c8abf
abd94d0
186c46b
e31a3a8
329c544
5404b6a
0f0008b
546c1bc
d80c640
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ Depends: | |
| Imports: | ||
| glue, | ||
| magrittr, | ||
| stringi (>= 0.4.1) | ||
| stringi (>= 1.1.6) | ||
| Suggests: | ||
| covr, | ||
| htmltools, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,8 @@ | |
| #' matrix to `start`. | ||
| #' | ||
| #' Negative values count backwards from the last character. | ||
| #' @param omit_na single logical value; if \code{TRUE}, missing values in any of the arguments | ||
| #' provided will result in an unchanged input; replacement function only | ||
|
||
| #' @param value replacement string | ||
| #' @return A character vector of substring from `start` to `end` | ||
| #' (inclusive). Will be length of longest input argument. | ||
|
|
@@ -50,6 +52,15 @@ | |
| #' str_sub(x, -1, -1) <- "K"; x | ||
| #' str_sub(x, -2, -2) <- "GHIJ"; x | ||
| #' str_sub(x, 2, -2) <- ""; x | ||
| #' | ||
| #' # If you want to keep the original if some argument is NA, | ||
| #' # use omit_na = TRUE | ||
| #' x1 <- x2 <- x3 <- x4 <- "AAA" | ||
| #' str_sub(x1, 1, NA) <- "B" | ||
| #' str_sub(x2, 1, 2) <- NA | ||
| #' str_sub(x3, 1, NA, omit_na = TRUE) <- "B" | ||
| #' str_sub(x4, 1, 2, omit_na = TRUE) <- NA | ||
| #' x1; x2; x3; x4 | ||
| str_sub <- function(string, start = 1L, end = -1L) { | ||
| if (is.matrix(start)) { | ||
| stri_sub(string, from = start) | ||
|
|
@@ -61,11 +72,11 @@ str_sub <- function(string, start = 1L, end = -1L) { | |
|
|
||
| #' @export | ||
| #' @rdname str_sub | ||
| "str_sub<-" <- function(string, start = 1L, end = -1L, value) { | ||
| "str_sub<-" <- function(string, start = 1L, end = -1L, omit_na = FALSE, value) { | ||
| if (is.matrix(start)) { | ||
| stri_sub(string, from = start) <- value | ||
| stri_sub(string, from = start, omit_na = omit_na) <- value | ||
| } else { | ||
| stri_sub(string, from = start, to = end) <- value | ||
| stri_sub(string, from = start, to = end, omit_na = omit_na) <- value | ||
| } | ||
| string | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please switch to markdown formatting?