Dear R Users, I am working with gsub for the first time. I am trying to remove some characters from a string. I have hit the problem where the period is the shorthand for 'everything' in the R language when what I want to remove is the actual periods. In the example below, I simply want to remove the periods as I have removed the comma, but instead the complete string is wiped out. I would appreciate it if someone could let me know how I communicate that I want to remove the period verbatim to R. Many thanks. --John Sparks> txt="This is a test. However, it is only a test." > txt2<-gsub(",","",txt) > txt2[1] "This is a test. However it is only a test."> txt3<-gsub(".","",txt) > txt3[1] "">
Hi John,
Try
gsub("[.]","",txt)
See "Extended Regular Expressions" in ?regex.
HTH,
Jorge
*
*
On Mon, Mar 21, 2011 at 12:49 AM, Sparks, John James <> wrote:
> Dear R Users,
>
> I am working with gsub for the first time. I am trying to remove some
> characters from a string. I have hit the problem where the period is the
> shorthand for 'everything' in the R language when what I want to
remove is
> the actual periods. In the example below, I simply want to remove the
> periods as I have removed the comma, but instead the complete string is
> wiped out. I would appreciate it if someone could let me know how I
> communicate that I want to remove the period verbatim to R.
>
> Many thanks.
> --John Sparks
>
> > txt="This is a test. However, it is only a test."
> > txt2<-gsub(",","",txt)
> > txt2
> [1] "This is a test. However it is only a test."
> > txt3<-gsub(".","",txt)
> > txt3
> [1] ""
> >
>
> ______________________________________________
> R-help@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.
>
[[alternative HTML version deleted]]
I think it is as simple as adding the argument fixed=TRUE to the function call.
txt = "this. is. a. test."
gsub(".", "", txt, fixed = TRUE)
result
[1] "this is a test"
HTH,
Peter
On Sun, Mar 20, 2011 at 9:49 PM, Sparks, John James <jspark4 at uic.edu>
wrote:> Dear R Users,
>
> I am working with gsub for the first time. ?I am trying to remove some
> characters from a string. ?I have hit the problem where the period is the
> shorthand for 'everything' in the R language when what I want to
remove is
> the actual periods. ?In the example below, I simply want to remove the
> periods as I have removed the comma, but instead the complete string is
> wiped out. ?I would appreciate it if someone could let me know how I
> communicate that I want to remove the period verbatim to R.
>
> Many thanks.
> --John Sparks
>
>> txt="This is a test. However, it is only a test."
>> txt2<-gsub(",","",txt)
>> txt2
> [1] "This is a test. However it is only a test."
>> txt3<-gsub(".","",txt)
>> txt3
> [1] ""
>>
>
> ______________________________________________
> 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 Sun, Mar 20, 2011 at 11:49:05PM -0500, Sparks, John James wrote:> Dear R Users, > > I am working with gsub for the first time. I am trying to remove some > characters from a string. I have hit the problem where the period is the > shorthand for 'everything' in the R language when what I want to remove is > the actual periods. In the example below, I simply want to remove the > periods as I have removed the comma, but instead the complete string is > wiped out. I would appreciate it if someone could let me know how I > communicate that I want to remove the period verbatim to R. > > Many thanks. > --John Sparks > > > txt="This is a test. However, it is only a test." > > txt2<-gsub(",","",txt) > > txt2 > [1] "This is a test. However it is only a test." > > txt3<-gsub(".","",txt) > > txt3 > [1] ""In order to force a "." to be interpreted literally, it is possible to use "\\." as follows gsub("\\.","",txt) [1] "This is a test However, it is only a test" Petr Savicky.