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.
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.
Try using the yearmon class in zoo:> library(zoo) > d <- c("1990m3", "1992m8") > ym <- as.yearmon(d, "%Ym%m") > ym[1] "Mar 1990" "Aug 1992"> as.Date(ym)[1] "1990-03-01" "1992-08-01" On Thu, Dec 18, 2008 at 1:14 AM, Shruthi Jayaram <shruthi.jayaram.85 at gmail.com> 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. >
Thanks so much everyone, these suggestions solve my problem in a very elegant
way.
A friend and I also came up with this "brute force" solution:
T <- c("1993m10")
y <- gsub("m.", "", T)
m <- gsub(".*m", "", T)
d <- paste(y,"-0",m,"-","01", sep="")
T <- as.Date(d, format="%Y-%m-%d")
Thought I would post it in any case.
Shruthi
--
View this message in context:
http://www.nabble.com/Parsing-unusual-date-format-tp21067562p21069970.html
Sent from the R help mailing list archive at Nabble.com.