1

I would like to replace all occurrences of "" in a vector with <NA>. Suppose,

 V<- c("","Axe","Saw","")

Then after replacement,

  V<- c("<NA>","Axe","Saw","<NA>")

Using gsub to replace a substring is easy but for searching "" I think I need to use / or \ inside "" but not sure how do do it.

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
user3922546
  • 187
  • 1
  • 6
  • 16
  • 2
    If you want to replace the empty strings by `NA`, you could do `V[V==''] <- NA`. I don't see the point in replacing by a character NA. ie. `""` Suppose your `V` is `V <- c('""', 'Axe', 'Saw', '""'); gsub("[\"]+", "", V)` – akrun Jan 25 '15 at 11:46
  • Never thought about it. Thanks. – user3922546 Jan 25 '15 at 11:57
  • If your vector in fact happens to be derived from a data frame, and the `""` appeared upon reading this data to R, you may also have a look at the `na.strings` argument in the `read.table/csv/xyz` functions. – Henrik Jan 25 '15 at 12:01

2 Answers2

4

You can use

is.na(V) <- V == ""

to replace empty strings with NA.

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
4

There is a function named after exactly what you are trying to do: replace().

With it, you can do:

> V<- c("","Axe","Saw","")
> V
[1] ""    "Axe" "Saw" ""   
> replace(V, V == "", NA)
[1] NA    "Axe" "Saw" NA 

That's like saying "replace in the vector 'V' any values equal to '""' with NA". So, it's not a word-for-word transcription of your question title, but it is pretty close :-)

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485