Dear all, I have a date related question. Suppose I have a character string "March-2009", how I can convert it to a valid date object like as.yearmon("2009-01-03") in the zoo package? Is there any possibility there? Ans secondly is there any R function which will give the names of of all months as "LETTERS" does? Thanks for your time. [[alternative HTML version deleted]]
?strptime ?%B? Full month name in the current locale. (Also matches abbreviated name on input.) On Wed, Jul 7, 2010 at 8:40 AM, Christofer Bogaso <bogaso.christofer at gmail.com> wrote:> Dear all, I have a date related question. Suppose I have a character string > "March-2009", how I can convert it to a valid date object like > as.yearmon("2009-01-03") in the zoo package? Is there any possibility there? > > Ans secondly is there any R function which will give the names of of all > months as "LETTERS" does? > > Thanks for your time. > > ? ? ? ?[[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. >
On Jul 7, 2010, at 7:40 AM, Christofer Bogaso wrote:> Dear all, I have a date related question. Suppose I have a character string > "March-2009", how I can convert it to a valid date object like > as.yearmon("2009-01-03") in the zoo package? Is there any possibility there? > > Ans secondly is there any R function which will give the names of of all > months as "LETTERS" does? > > Thanks for your time.You may need to append a default day, so something like:> as.Date(paste("March-2009", "-15", sep = ""), format = "%B-%Y-%d")[1] "2009-03-15" Otherwise on OSX, I get:> as.Date("March-2009", format = "%B-%Y")[1] NA The above behavior regarding missing components is system specific, as per the Note in ?as.Date. Also see ?strptime for the formatting options. For the second part:> month.name[1] "January" "February" "March" "April" "May" [6] "June" "July" "August" "September" "October" [11] "November" "December" See ?LETTERS HTH, Marc Schwartz
On Wed, Jul 7, 2010 at 8:40 AM, Christofer Bogaso <bogaso.christofer at gmail.com> wrote:> Dear all, I have a date related question. Suppose I have a character string > "March-2009", how I can convert it to a valid date object like > as.yearmon("2009-01-03") in the zoo package? Is there any possibility there?Try this. The first returns a Date class object and the second returns a character string:> d <- "March-2009" > as.Date(as.yearmon(d, "%B-%Y"))[1] "2009-03-01"> format(as.Date(as.yearmon(d, "%B-%Y")))[1] "2009-03-01" See R News 4/1 for more on times and dates.> > Ans secondly is there any R function which will give the names of of all > months as "LETTERS" does?> month.abb[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"> month.name[1] "January" "February" "March" "April" "May" "June" [7] "July" "August" "September" "October" "November" "December"