Sean Zhang
2009-Sep-22 19:03 UTC
[R] how to convert character string with only month and year into date
Dear R helpers. I am new to plotting time data using R. wonder how to convert character time info into date in R. I searched over the web but did not find answer. the input character string is something like 03_1993 or 03-1993, so the precision is at month level. I tried the following but failed. #R code below. strptime(c("03_1993"),"%m_%Y") strptime(c("03-1993"),"%m-%Y") Can you someone kindly show me to do it? Many thanks in advance! -Sean [[alternative HTML version deleted]]
Henrique Dallazuanna
2009-Sep-22 19:11 UTC
[R] how to convert character string with only month and year into date
Try this: strptime(paste(1, c("03_1993")),"%d %m_%Y") strptime(paste(1, c("03-1993")),"%d %m_%Y") On Tue, Sep 22, 2009 at 4:03 PM, Sean Zhang <seanecon at gmail.com> wrote:> Dear R helpers. > > I am new to plotting time data using R. > wonder how to convert character time info into date in R. > I searched over the web but did not find answer. > > the input character string is something like 03_1993 or 03-1993, so the > precision is at month level. ?I tried the following but failed. > #R code below. > > ?strptime(c("03_1993"),"%m_%Y") > strptime(c("03-1993"),"%m-%Y") > > Can you someone kindly show me to do it? > > Many thanks in advance! > > -Sean > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Gabor Grothendieck
2009-Sep-22 19:19 UTC
[R] how to convert character string with only month and year into date
Try this:> library(zoo) # as.yearmon > > as.yearmon("03_1993", "%m_%Y")[1] "Mar 1993"> as.Date(as.yearmon("03_1993", "%m_%Y"))[1] "1993-03-01" On Tue, Sep 22, 2009 at 3:03 PM, Sean Zhang <seanecon at gmail.com> wrote:> Dear R helpers. > > I am new to plotting time data using R. > wonder how to convert character time info into date in R. > I searched over the web but did not find answer. > > the input character string is something like 03_1993 or 03-1993, so the > precision is at month level. ?I tried the following but failed. > #R code below. > > ?strptime(c("03_1993"),"%m_%Y") > strptime(c("03-1993"),"%m-%Y") > > Can you someone kindly show me to do it? > > Many thanks in advance! > > -Sean > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >
David Winsemius
2009-Sep-22 19:19 UTC
[R] how to convert character string with only month and year into date
On Sep 22, 2009, at 3:03 PM, Sean Zhang wrote:> Dear R helpers. > > I am new to plotting time data using R. > wonder how to convert character time info into date in R. > I searched over the web but did not find answer. > > the input character string is something like 03_1993 or 03-1993, so > the > precision is at month level. I tried the following but failed. > #R code below. > > strptime(c("03_1993"),"%m_%Y") > strptime(c("03-1993"),"%m-%Y") > > Can you someone kindly show me to do it? >The usual R classes do not have a year-month version but package zoo does: > library(zoo) > as.yearmon("03_1993","%m_%Y") [1] "Mar 1993">-- David Winsemius, MD Heritage Laboratories West Hartford, CT