How do I replace an empty string?
This:
x = c("","b")
gsub("","taco",x)
produces:
"taco" "tacobtaco"
instead of:
"taco" "b"
Is there any way to replace an empty string?
I would use nchar
here:
x[nchar(x)==0] <- "taco"
EDIT
If you are looking for performance so you should use nzchar:
x[!nzchar(x)] <- "taco"
x = c("","b")
gsub("^$","taco",x)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With