Find the last day of the month

Asked

Viewed 398 times

7

At the beginning of the month I always need to calculate the age of people within a certain database. I always consider the last date of that month, for example: in September I calculated the age using the date of 30 September as a reference, so everyone who was born within this month I consider with one year more complete. Is there a function that will return me the final date of a given month so I can automate the task, or I will have to do it manually?

1 answer

10


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

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

It uses the function %m+% package lubridate. 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 in an English OS topic.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.