Is there any R function to calculate automatically the last day of a particular month? For example "sep2009" should be converted to last day of September of 2009? Thanks -- View this message in context: http://www.nabble.com/How-to-get-last-day-of-a-month--tp25425645p25425645.html Sent from the R help mailing list archive at Nabble.com.
Does this help. Shows how to use the basic functions to get at the answer:> # add first day of month to make it valid > x <- as.POSIXct(paste('1', 'sep2009', sep=''), format="%d%b%Y") > # now advance one month and then go back one day for the end of the month > next.mon <- seq(x, length=2, by='1 month')[2] > last.day <- seq(next.mon, length=2, by='-1 day')[2] > last.day[1] "2009-09-30 GMT">On Sun, Sep 13, 2009 at 2:18 PM, megh <megh700004 at yahoo.com> wrote:> > Is there any R function to calculate automatically the last day of a > particular month? For example "sep2009" should be converted to last day of > September of 2009? > > Thanks > -- > View this message in context: http://www.nabble.com/How-to-get-last-day-of-a-month--tp25425645p25425645.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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Try this:> library(zoo) # as.yearqtr > > # test data > x <- c("sep2009", "oct2009") > > # convert to "yearqtr" class and from that to Date using frac = 1 > # where frac is between 0 and 1 inclusive indicating where > # in the month the output date should be set to > > as.Date(as.yearqtr(x, "%b%Y"), frac = 1)[1] "2009-09-30" "2009-12-31" On Sun, Sep 13, 2009 at 2:18 PM, megh <megh700004 at yahoo.com> wrote:> > Is there any R function to calculate automatically the last day of a > particular month? For example "sep2009" should be converted to last day of > September of 2009? > > Thanks > -- > View this message in context: http://www.nabble.com/How-to-get-last-day-of-a-month--tp25425645p25425645.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. >
That should have been yearmon (not yearqtr):> library(zoo) # as.yearqtr > > # test data > x <- c("sep2009", "oct2009") > > # convert to "yearmon" class and from that to Date using frac = 1 > # where frac is between 0 and 1 inclusive indicating where > # in the month the output date should be set to > > as.Date(as.yearmon(x, "%b%Y"), frac = 1)[1] "2009-09-30" "2009-10-31" On Sun, Sep 13, 2009 at 2:51 PM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> Try this: > >> library(zoo) # as.yearqtr >> >> # test data >> x <- c("sep2009", "oct2009") >> >> # convert to "yearqtr" class and from that to Date using frac = 1 >> # where frac is between 0 and 1 inclusive indicating where >> # in the month the output date should be set to >> >> as.Date(as.yearqtr(x, "%b%Y"), frac = 1) > [1] "2009-09-30" "2009-12-31" > > On Sun, Sep 13, 2009 at 2:18 PM, megh <megh700004 at yahoo.com> wrote: >> >> Is there any R function to calculate automatically the last day of a >> particular month? For example "sep2009" should be converted to last day of >> September of 2009? >> >> Thanks >> -- >> View this message in context: http://www.nabble.com/How-to-get-last-day-of-a-month--tp25425645p25425645.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. >> >