Skip to content

given a mid-month date, get the month-end date

5 messages · t c, jim holtman, Gabor Grothendieck +1 more

#
The zoo package has a yearmon class with as methods which can be
used:

library(zoo)
dd <- Sys.Date()  # test data

as.Date(as.yearmon(dd), frac = 1)

as.yearmon converts the "Date" class date to a year and month of
class "yearmon" dropping the day and representing it internally in
a way consistent with "ts" class.

as.Date above then converts it back to "Date" class.
Since yearmon dates have no day (they are just a year and
a month) the frac argument is used to indicate what fraction
of the month to use as the day of the month so frac = 0 (the
default) would give the beginning of the month) and frac = 1
gives the end.
On 12/19/05, t c <quantpm at yahoo.com> wrote:
#
If you work much with time data and time series data and you have not 
already mastered the "zoo" package, you may be interested in a small 
testimonial from one unfamiliar with the names of Achim Zeileis and 
Gabor Grothendieck two years ago:  The package itself seems to have many 
useful features, filling an important gap in other capabilities, AND 
vignette("zoo") makes it relatively easy to learn.

	  Those unfamiliar with vignettes in R may wish to know that the command

	> (zoo.vignette <- vignette("zoo"))

will open an Adobe Acrobat file containing a narrative overview of the 
package's capabilities.  The R commands used in the file can be easily 
obtained in a script file after this command via

	  > edit(zoo.vignette)

in RGui;  this should open the R comands in an editor script window. 
This may not have the desired effect in XEmacs, which I use.  Instead, I 
use the following:

	  > Stangle(zoo.vignette$file)

This writes the R commands to file zoo.R in the working directory.  From 
there, I then use menu File -> Open -> ?zoo.R?.

	  hope this helps.
	  Spencer Graves
p.s.  In case it's not completely obvious from the above, I wish to here 
extend my thanks and complements to Achim Zeileis and Gabor Grothendieck 
for a great package with good documentation.
Gabor Grothendieck wrote: