An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20091123/36ef28cf/attachment-0001.pl>
Removing "+" and "?" signs
8 messages · Steven Kang, jim holtman, Linlin Yan +4 more
'?' is a metacharacter in a regular expression. You have to escape it:
x <- "asdf+,jkl?"
gsub("?", " ", x)
Error in gsub("?", " ", x) : invalid regular expression '?'
In addition: Warning message:
In gsub("?", " ", x) :
regcomp error: 'Invalid preceding regular expression'
# escape it
gsub("\\?", " ", x)
[1] "asdf+,jkl "
On Sun, Nov 22, 2009 at 6:01 PM, Steven Kang <stochastickang at gmail.com> wrote:
Hi all,
I get an error message when trying to replace *+* or *?* signs (with empty
space) from a string.
x <- "asdf+,jkl?"
gsub("?", " ", x)
Error message:
Error in
gsub("?", " ", x) :
?invalid regular expression '?'
In addition: Warning message:
In gsub("?", " ", x) :
?regcomp error: ?'Invalid preceding regular expression'
Your expertise in resolving this issue would be appreciated.
Thanks.
Steven
? ? ? ?[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list 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.
Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Try this:
gsub("[?]", " ", x)
On Mon, Nov 23, 2009 at 7:01 AM, Steven Kang <stochastickang at gmail.com> wrote:
Hi all,
I get an error message when trying to replace *+* or *?* signs (with empty
space) from a string.
x <- "asdf+,jkl?"
gsub("?", " ", x)
Error message:
Error in
gsub("?", " ", x) :
?invalid regular expression '?'
In addition: Warning message:
In gsub("?", " ", x) :
?regcomp error: ?'Invalid preceding regular expression'
Your expertise in resolving this issue would be appreciated.
Thanks.
Steven
? ? ? ?[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list 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.
On 23/11/2009, at 12:01 PM, Steven Kang wrote:
Hi all,
I get an error message when trying to replace *+* or *?* signs
(with empty
space) from a string.
x <- "asdf+,jkl?"
gsub("?", " ", x)
Error message:
Error in
gsub("?", " ", x) :
invalid regular expression '?'
In addition: Warning message:
In gsub("?", " ", x) :
regcomp error: 'Invalid preceding regular expression'
Your expertise in resolving this issue would be appreciated.
(a) That's funny. I don't get an error message when I try your example.
I get
[1] " a s d f + , j k l ? "
Of course that's not what you want, though.
(b) You need to escape the question mark:
> gsub("\\?", " ", x)
yields
[1] "asdf+,jkl "
which I think *is* what you want.
cheers,
Rolf Turner
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20091122/d0c5e069/attachment-0001.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20091122/77d86634/attachment-0001.pl>
On Nov 22, 2009, at 6:01 PM, Steven Kang wrote:
Hi all,
I get an error message when trying to replace *+* or *?* signs (with
empty
space) from a string.
x <- "asdf+,jkl?"
gsub("?", " ", x)
Since both ? and + are special regex characters, to do both the
substitutions at once you need to use double backslashes and an <or>
> gsub("\\?|\\+", " ", x)
[1] "asdf ,jkl "
David
>
>
> Error message:
>
> Error in
> gsub("?", " ", x) :
> invalid regular expression '?'
> In addition: Warning message:
> In gsub("?", " ", x) :
> regcomp error: 'Invalid preceding regular expression'
>
> Your expertise in resolving this issue would be appreciated.
>
> Thanks.
>
>
>
> Steven
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
Jorge Ivan Velez wrote:
And if you want to replace both "+" and "?", here is a suggestion:
x <- "asdf+,jkl?"
gsub("[?]|[+]", "", x)
# [1] "asdf,jkl"
Once you're into character classes ([....]), you might as well go all
the way:
> gsub("[?+]", "", x)
[1] "asdf,jkl"
O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907