Simple regex problem

View: New views
4 Messages — Rating Filter:   Alert me  

Simple regex problem

by John Kane-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I am cleaning up some character data to convert to
dates using chron. The regex that I am using work fine
but there should be a better way to do this. Can
anyone suggest a cleaner bit of code than the gsub,
sub approach? Thanks

#Test Run on dates.
xx <- c("May  1, 2007",
"May  2, 2007",
"May  3, 2007",
"May  4, 2007",
"May  5, 2007",
"May  6, 2007",
"May  7, 2007",
"May  8, 2007",
"May  9, 2007",
"May 10, 2007",
"May 11, 2007",
"May 12, 2007")
yy <- gsub(" +", "/", xx) ; yy
zz <- sub(",", "", yy) ; zz
library(chron)
ttdates <- chron(zz, format = c(dates = "mon/d/y"))

______________________________________________
R-help@... 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.

Re: Simple regex problem

by Ted.Harding-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 10-May-08 16:49:20, John Kane wrote:

> I am cleaning up some character data to convert to
> dates using chron. The regex that I am using work fine
> but there should be a better way to do this. Can
> anyone suggest a cleaner bit of code than the gsub,
> sub approach? Thanks
>
>#Test Run on dates.
> xx <- c("May  1, 2007",
> "May  2, 2007",
> "May  3, 2007",
> "May  4, 2007",
> "May  5, 2007",
> "May  6, 2007",
> "May  7, 2007",
> "May  8, 2007",
> "May  9, 2007",
> "May 10, 2007",
> "May 11, 2007",
> "May 12, 2007")
> yy <- gsub(" +", "/", xx) ; yy
> zz <- sub(",", "", yy) ; zz

You can do it in one stroke with
gsub(",*[ ]+", "/", xx)
 [1] "May/1/2007"  "May/2/2007"  "May/3/2007"  "May/4/2007"
 [5] "May/5/2007"  "May/6/2007"  "May/7/2007"  "May/8/2007"
 [9] "May/9/2007"  "May/10/2007" "May/11/2007" "May/12/2007"

> library(chron)
> ttdates <- chron(zz, format = c(dates = "mon/d/y"))

Best wishes,
Ted

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding@...>
Fax-to-email: +44 (0)870 094 0861
Date: 10-May-08                                       Time: 18:14:33
------------------------------ XFMail ------------------------------

______________________________________________
R-help@... 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.

Re: Simple regex problem

by Gabor Grothendieck :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Try this (no gsub required):

as.chron(as.Date(xx, "%b %d, %Y"))

On Sat, May 10, 2008 at 12:49 PM, John Kane <jrkrideau@...> wrote:

> I am cleaning up some character data to convert to
> dates using chron. The regex that I am using work fine
> but there should be a better way to do this. Can
> anyone suggest a cleaner bit of code than the gsub,
> sub approach? Thanks
>
> #Test Run on dates.
> xx <- c("May  1, 2007",
> "May  2, 2007",
> "May  3, 2007",
> "May  4, 2007",
> "May  5, 2007",
> "May  6, 2007",
> "May  7, 2007",
> "May  8, 2007",
> "May  9, 2007",
> "May 10, 2007",
> "May 11, 2007",
> "May 12, 2007")
> yy <- gsub(" +", "/", xx) ; yy
> zz <- sub(",", "", yy) ; zz
> library(chron)
> ttdates <- chron(zz, format = c(dates = "mon/d/y"))
>
> ______________________________________________
> R-help@... 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.
>

______________________________________________
R-help@... 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.

Re: Simple regex problem

by John Kane-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks to Gabor Grothendieck  & Ted Harding.  both
solutions work very nicely.  



--- Gabor Grothendieck <ggrothendieck@...>
wrote:

> Try this (no gsub required):
>
> as.chron(as.Date(xx, "%b %d, %Y"))
>
> On Sat, May 10, 2008 at 12:49 PM, John Kane
> <jrkrideau@...> wrote:
> > I am cleaning up some character data to convert to
> > dates using chron. The regex that I am using work
> fine
> > but there should be a better way to do this. Can
> > anyone suggest a cleaner bit of code than the
> gsub,
> > sub approach? Thanks
> >
> > #Test Run on dates.
> > xx <- c("May  1, 2007",
> > "May  2, 2007",
> > "May  3, 2007",
> > "May  4, 2007",
> > "May  5, 2007",
> > "May  6, 2007",
> > "May  7, 2007",
> > "May  8, 2007",
> > "May  9, 2007",
> > "May 10, 2007",
> > "May 11, 2007",
> > "May 12, 2007")
> > yy <- gsub(" +", "/", xx) ; yy
> > zz <- sub(",", "", yy) ; zz
> > library(chron)
> > ttdates <- chron(zz, format = c(dates =
> "mon/d/y"))
> >
> > ______________________________________________
> > R-help@... 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.
> >
>



      __________________________________________________________________
[[elided Yahoo spam]]

______________________________________________
R-help@... 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.