Saturday, April 23, 2011

Dates in R and the First Day of the Month

I spent some time this morning learning about how R thinks about dates in R. I found this website to be a useful guide.

Imagine that your data are dates in a standard format [YEAR-MONTH-DAY (as in 2011-23-04) is one such format] and you want a vector of the dates that are the first day in the month of your data set. You might, for example, be interested in this (as an intermediate calculation) because you wish to match some S&P 500 returns data to monthly data. Frustratingly, the S&P 500 returns data do not report on the weekends or on holidays when the exchange is closed.

Here's a function that will do return a vector of first-days-of-the-month if you give it a vector of dates. Maybe you will find this useful.



  1. Thank you, tony. I am trying to match CRSP and compustat data as we speak. I hope this will be helpful. Do you have any more tips that may help (an old post, website you could link me to)? Thanks

  2. Before this post, I hadn't done anything on this blog about merging data. In the next couple of days, my plan is to post something on merging data frames in R. My application will be with the FRED data, but perhaps, you'll find it useful.