Detect the last day of the month

6

At the beginning of the month I always need to calculate the age of the people within a given database. I always do considering the last date of that month, for example: in September I calculated the age using September 30th as a reference, so everyone born within this month I consider a fuller year. Is there a function that returns me the end date of a given month so I can automate the task, or will I have to do it manually?

    
asked by anonymous 17.10.2017 / 14:48

1 answer

9

With the following function, you get the last days of the months following a given last day of the month

seq_monthly <- function(from,length.out) {
  return(from %m+% months(c(0:(length.out-1))))
}

It uses the %m+% function of the lubridate package. You can then create a vector with all the last days of a given year

seq_monthly(as.Date("????-01-31"),length.out=12)

and index within its function the reference month. I found this function on an English OS topic.

    
17.10.2017 / 15:11