Hi, Is there any function that replaces a dot with a space? I expect "c t" from the output of the second call of function sub, but it did not do so.> sub("a", "b", "cat")[1] "cbt"> sub(".", " ", "c.t")[1] " .t" Thanks! [[alternative HTML version deleted]]
Omar André Gonzáles Díaz
2016-Nov-19 00:20 UTC
[R] Replace a dot in a string (".") with a space (" ")
Hi, your were close.
This is the solution:
sub("[.]", " ", "c.t")
You need to scape the point, because point has a especial meaning in
regular expressions. Read more on regex...
2016-11-18 19:13 GMT-05:00 John <miaojpm at gmail.com>:
> Hi,
>
> Is there any function that replaces a dot with a space? I expect "c
t"
> from the output of the second call of function sub, but it did not do so.
>
> > sub("a", "b", "cat")
> [1] "cbt"
> > sub(".", " ", "c.t")
> [1] " .t"
>
> Thanks!
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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]]
Well of course. See ?regexp where it says: "The period . matches any single character. " (Always a good idea to read man pages, although this *is* a complex one) Setting the fixed argument to TRUE is one way to get what you want (there are others).> sub(".", " ", "c.t",fixed=TRUE)[1] "c t" Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Nov 18, 2016 at 4:13 PM, John <miaojpm at gmail.com> wrote:> Hi, > > Is there any function that replaces a dot with a space? I expect "c t" > from the output of the second call of function sub, but it did not do so. > >> sub("a", "b", "cat") > [1] "cbt" >> sub(".", " ", "c.t") > [1] " .t" > > Thanks! > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
And here is another.
sub("\\.", " ", "c.t")
Rui Barradas
Em 19-11-2016 01:35, Bert Gunter escreveu:> Well of course. See ?regexp where it says:
>
> "The period . matches any single character."
>
> (Always a good idea to read man pages, although this *is* a complex one)
>
> Setting the fixed argument to TRUE is one way to get what you want
> (there are others).
>
>> sub(".", " ", "c.t",fixed=TRUE)
> [1] "c t"
>
> Bert
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip
)
>
>
> On Fri, Nov 18, 2016 at 4:13 PM, John <miaojpm at gmail.com> wrote:
>> Hi,
>>
>> Is there any function that replaces a dot with a space? I expect
"c t"
>> from the output of the second call of function sub, but it did not do
so.
>>
>>> sub("a", "b", "cat")
>> [1] "cbt"
>>> sub(".", " ", "c.t")
>> [1] " .t"
>>
>> Thanks!
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>
> ______________________________________________
> 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.
>