Skip to content
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

str_replace() isn't able to replace strings with NAs, since 1.1.0 #124

Closed
filipsch opened this issue Aug 23, 2016 · 6 comments
Closed

str_replace() isn't able to replace strings with NAs, since 1.1.0 #124

filipsch opened this issue Aug 23, 2016 · 6 comments

Comments

@filipsch
Copy link

In stringr version 1.0.0, the following worked fine:

> str_replace(c(NA, "abc", ""), pattern = "^$", NA)
[1] NA    "abc" NA   

In stringr version 1.1.0, an error is thrown:

> str_replace(c(NA, "abc", ""), pattern = "^$", NA)
Error in if (char == "$") { : missing value where TRUE/FALSE needed

Is this updated behavior intentional?

@hadley
Copy link
Member

hadley commented Aug 23, 2016

I don't think that should have ever worked — but it does need a better error message.

@denrou
Copy link

denrou commented Dec 30, 2016

Is there a reason for this behavior?

Here's an example of some data I'm working on:

d> glimpse(raw_data$survey)
Observations: 3,497
Variables: 21
$ site_surface              <chr> "No Value", "No Value", "No Value", "No Value", "No Value", "No Value", "No Value", "No Val...
$ location_name             <chr> "Bâtiment AP1", "Site Romans", "No Value", "No Value", "No Value", "No Value", "No Value", ...
$ location_surface          <chr> "No Value", "No Value", "No Value", "No Value", "No Value", "No Value", "No Value", "No Val...
$ stats                     <chr> "Solved", "Solved", "Solved", "Solved", "Solved", "Solved", "Solved", "Solved", "Completed"...
$ value                     <chr> "No Value", "No Value", "No Value", "No Value", "No Value", "No Value", "No Value", "No", "...
...

Something like mutate_all(raw_data$survey, str_replace, pattern = "No Value", replacement = NA) would definitely be helpful in this case.

For now, I'm using this workaround:

  tidy_gsub <- function(x, pattern, replacement, ...) {
    gsub(pattern, replacement, x, ...)
  }

 raw_data$survey %>%
   mutate_all(tidy_gsub, pattern = "No Value", replacement = NA)

@hadley
Copy link
Member

hadley commented Dec 30, 2016

You'd be better off with if_else plus str_detect. str_replace() has to replace parts of a string and replacing part of a string with NA doesn't make sense.

@denrou
Copy link

denrou commented Dec 30, 2016

I see your point. What do you think of an approach à la tidyr::replace_na:

replace_no_value <- function(data, no_value = "") {
  for (var in names(data)) {
    data[[var]][data[[var]] == no_value] <- NA
  }
  data
}

@hadley hadley closed this as completed in 90c29f0 Dec 30, 2016
@hadley
Copy link
Member

hadley commented Dec 30, 2016

stringi seems to support it, so I implemented it anyway (you'll need to use NA_character_ though)

@denrou
Copy link

denrou commented Dec 30, 2016

Perfect! Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants