Create end of the month date from a date variable

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
date.end.month
[1] "2012-01-31" "2012-02-29" "2012-03-31" "2012-04-30"

Here is another solution using the lubridate package:

date.start.month=seq(as.Date("2012-01-01"),length=4,by="months")
df=data.frame(date.start.month)

library(lubridate)
df$date.end.month <- ceiling_date(df$date.start.month, "month") - days(1)
df$date.end.month
[1] "2012-01-31" "2012-02-29" "2012-03-31" "2012-04-30"

This uses the same concept given by James above, in that it gets the first day of the next month and subtracts one day.

By the way, this will work even when the input date is not necessarily the first day of the month. So for example, today is the 27th of the month and it still returns the correct last day of the month:

ceiling_date(Sys.Date(), "month") - days(1)
[1] "2017-07-31"

Use timeLastDayInMonth from the timeDate package:

df$eom<-timeLastDayInMonth(df$somedate)

library(lubridate)
 as.Date("2019-09-01") - days(1)
[1] "2019-08-31"

or

library(lubridate)
 as.Date("2019-09-01") + months(1) - days(1)
[1] "2019-09-30"