[R] gsub() could not replace my string

William Dunlap wdunlap at tibco.com
Fri Oct 7 22:10:45 CEST 2016


Either backslash the square brackets, as they have a special meaning
(character range) in regular expressions, or use the fixed=TRUE argument to
indicate that your pattern is not a regular expression.
> gsub("\\[NA\\]NA%", "", "[NA]NA%abcde")
[1] "abcde"
> gsub("[NA]NA%", "", "[NA]NA%abcde", fixed=TRUE)
[1] "abcde"

Your original code would replace ANA% or NNA% with a blank.
> gsub("[NA]NA%", "", c("NNA%abcde", "ANA%abcde"))
[1] "abcde" "abcde"




Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Fri, Oct 7, 2016 at 12:58 PM, Christofer Bogaso <
bogaso.christofer at gmail.com> wrote:

> Hello again,
>
> I have few many string elements, and some of those elements contain a
> special phrase as '"[NA]NA%", which I planned to replace by some
> Blank. So I tried with below code in R
>
> > gsub("[NA]NA%", "", "[NA]NA%abcde")
> [1] "[NA]NA%abcde"
>
> It appears that, my code could not replace "[NA]NA%" as I wanted to
> see "abcde". Can you please help me with what would be the correct
> code for doing so.
>
> Thanks in advance
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list