[R] Year and month from a sequence

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Wed Jul 15 09:20:29 CEST 2020


Hello,

Yet another way, with package lubridate.
When creating the "Date" objects, I set day = 1 to compare to TRUE below.


library(lubridate)

start <- as.Date(ISOdate(1861, 1, 1))
end <- as.Date(ISOdate(2005, 12, 1))

start + months(4139 - 2400)
#[1] "2005-12-01"

end == start + months(4139 - 2400)
#[1] TRUE


The argument to function months can be a vector of integers.


start + months(1:(4139 - 2400))



Then extract the months.

month(start)
month(end)
month(start + months(<value>))
month(start + months(4139 - 2400))


Hope this helps,

Rui Barradas

Às 23:26 de 14/07/20, Miluji Sb escreveu:
> 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