Skip to content

Parsing unusual date format

2 messages · Mark Leeds, Phil Spector

#
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:

            
#
You can use regular expressions:
as.Date(sub('(\\d+)m(\\d+)','\\1-\\2-01',dts,perl=TRUE))

but as.Date isn't as inflexible as you think:
as.Date(paste(dts,'m01',sep=''),'%Ym%mm%d')

                                        - Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu
On Thu, 18 Dec 2008, markleeds at verizon.net wrote: