tidyverse / stringr

A fresh approach to string manipulation in R

Home Page:https://stringr.tidyverse.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Behavior of str_remove when called with NA_character_ pattern

davidciani opened this issue · comments

When called with NA as a pattern, str_remove fails loudly about a type mismatch. When called with NA_character_ as a pattern, it silently returns NA. Is this the expected behavior? If so it doesn't seem to be documented anywhere. Personally, I was expecting that str_remove would have returned the input string unmodified in this scenario (as there is nothing specified to remove), or alternatively failed with an error message.

library(stringr)

str_remove("test", NA)
#> Error in `str_replace()`:
#> ! `pattern` must be a string, not `NA`.

str_remove("test", NA_character_)
#> [1] NA

# What lead to discovery of this, though str_c is behaving as expected returning NA_character_

string_to_remove = NA # a blank cell in a data frame

str_remove_all("test", str_c("\\b",string_to_remove,"\\b"))
#> [1] NA

Created on 2024-04-11 with reprex v2.1.0