[R] Parsing unusual date format

markleeds at verizon.net markleeds at verizon.net
Thu Dec 18 07:43:11 CET 2008


hi: i tried regular expressions and , as usual, failed. but below does 
do the job uglily using strsplit. i'd still be curious and appreciate if 
someone could do the regular expression method. thanks.

dts <- c("1990m12", "1992m8") #March 1990 and Aug 1992

#SPLIT IT USING m
temp <- strsplit(dts,"m")

# PASTE THE #'S BUT CHECK
# FOR GREATER THAN 9 UGLIFIES IT
moddates <- lapply(temp,function(.str) {
   if ( as.numeric(.str[2]) > 9 ) {
     paste(.str[1],as.numeric(.str[2]),"01",sep="-")
   } else {
     paste(.str[1],"-0",as.numeric(.str[2]),"-01",sep="")
   }
})

# MAKE  THE DATE
datelist<-lapply(moddates,as.Date ,format="%Y-%m-%d")




On Thu, Dec 18, 2008 at  1:14 AM, Shruthi Jayaram wrote:

> Hello,
>
> If I have a character string like
>
> d <- c("1990m3", "1992m8") #March 1990 and Aug 1992
>
> what is the easiest way to convert it into any standard date form; for
> example,
>
> d <- c("01/03/1990", "01/08/1992")
> I looked at as.Date but it doesn't seem to address my problem as I 
> have an
> "m" stuck in the middle of my character string which R does not 
> recognise.
>
> Would be very grateful for any help on this.
> Shruthi
> -- 
> View this message in context: 
> http://www.nabble.com/Parsing-unusual-date-format-tp21067562p21067562.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.



More information about the R-help mailing list