MikeTP MikeTP - 2 months ago 4
R Question

Method to create end of month date from date variable in [r] data frame

I have a [r] large data frame with date variables, which reflect first day of the month. Is the a easy way to crete a new data frame date variable that represents the last day of the month?

Below is some sample data:


"2012-01-01" "2012-02-01" "2012-03-01" "2012-04-01"

I would like to return a new variable wtih:

"2012-01-31" "2012-02-29" "2012-03-30" "2012-04-27"

I've tried the follwing but it was unsucessful:

df$date.end.month=seq(df$date.start.month,length=1,by="+1 months")

Any guidance to this new [r] user would be greatly appreciated.


To get the end of months you could just create a Date vector containing the 1st of all the subsequent months and subtract 1 day.

date.end.month <- seq(as.Date("2012-02-01"),length=4,by="months")-1
[1] "2012-01-31" "2012-02-29" "2012-03-31" "2012-04-30"