2

MOst search results give me the reverse of this, turn NULL or NA into a string 'NA'. I don't want that, I want to turn string instances of 'NULL' into NA but am getting an error:

bla <- c('foo', 'bar', NA, 'NULL')

str_replace_all(bla, 'NULL', NA)
Error: `replacement` must be a character vector

Also tried:

str_replace_all(bla, 'NULL', NA_real_)
Error: `replacement` must be a character vector

How can I convert cases of 'NULL' into NA in bla?

[edit]

To be explicit, I'm actually doing this within a dplyr chain e.g.

bla <- data.frame(s = c('foo', 'bar', NA, 'NULL'), n = 1:4 )
> bla
     s n
1  foo 1
2  bar 2
3 <NA> 3
4 NULL 4
> bla %>% mutate(s = str_replace_all(bla, 'NULL', NA_real_))
Error: Problem with `mutate()` input `s`.
x `replacement` must be a character vector
ℹ Input `s` is `str_replace_all(bla, "NULL", NA_real_)`.
Doug Fir
  • 19,971
  • 47
  • 169
  • 299

1 Answers1

5

Just use == instead of regex substring replacement

bla[bla == "NULL"] <- NA

-output

bla
[1] "foo" "bar" NA    NA   

With str_replace/str_replace_all, specify the correct type for NA (as by default, NA is logical which would have a type clash with the original vector type

According to ?NA

NA is a logical constant of length 1 which contains a missing value indicator. NA can be coerced to any other vector type except raw. There are also constants NA_integer_, NA_real_, NA_complex_ and NA_character_ of the other atomic vector types which support missing values: all of these are reserved words in the R language.

str_replace_all(bla, 'NULL', NA_character_)
[1] "foo" "bar" NA    NA   

Also, str_replace are mostly used for substring replacement and not for fixed complete string replacement (as we may find a decrease in efficiency as well)


In the tidyverse, there is also na_if

library(dplyr)
bla %>% 
    mutate(s = na_if(s, "NULL"))
     s n
1  foo 1
2  bar 2
3 <NA> 3
4 <NA> 4
akrun
  • 874,273
  • 37
  • 540
  • 662