To get the last day of the month, I used the following statement:
as.Date(format(as.Date(format(as.Date(20090323,%Y%m%d), %Y%m01),
%Y%m%d)+31,%Y%m01), %Y%m%d)-1
Of course, 20090323 is the original mmdd input date here. It's not
very sophisticated, but gets the job done in a simple way
jim holtman wrote:
just use timeDate from Rmetrics, there you will find several functions
like ...
# timeLastDayInMonthComputes the last day in a given month and year
# timeFirstDayInMonth Computes the first day in a given month and
year
# timeLastDayInQuarter Computes
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
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
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]
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
6 matches
Mail list logo