[R] Year and month from a sequence

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Wed Jul 15 00:58:51 CEST 2020


Hi Milu,
This is similar to the "birthday age" problem as it is much easier to
deal with the constant "12 months in a year" rather than the highly
variable number of days between two dates:

monthno2my<-function(x,startyear=1861,startmonth=2400) {
 year<-startyear+(x-startmonth)%/%12
 month<-1+(x-startmonth)%%12
 return(paste(month.abb[month],year))
}

Adjust this to suit what sort of output you want (i.e. numeric, date).

Jim

On Wed, Jul 15, 2020 at 8:26 AM Miluji Sb <milujisb using gmail.com> wrote:
>
> Dear all,
> I have a panel dataset with a large number of groups (200K+) with a
> sequence representing the month of the observations (2400 - 4139). The data
> is from January 1861 to December 2005.
> My goal is to extract the corresponding month and year for each observation.
>
> I tried the following;
> x$dt <- seq(from=as.Date("1861-01-01"),by='1 month',length=209000)
>
> However, the end date is clearly out of the range. I would like to convert
> the month number (2400 - 4139) to January 1861 to December 2005 by ID. How
> can I achieve this? Any help will be highly appreciated.
>
> Best,
>
> Milu
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list