Re: [R] Year and month from a sequence

2020-07-16 Thread Miluji Sb
Dear Jim, Eric, and Rui, Great solutions! Thank you so much. Best, Milu On Wed, Jul 15, 2020 at 9:20 AM Rui Barradas wrote: > Hello, > > Yet another way, with package lubridate. > When creating the "Date" objects, I set day = 1 to compare to TRUE below. > > > library(lubridate) > > start <- a

Re: [R] Year and month from a sequence

2020-07-15 Thread Rui Barradas
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 - 2

Re: [R] Year and month from a sequence

2020-07-15 Thread Eric Berger
Hi Milu, Jim gave a good solution. Another approach is to use standard packages and objects. In particular 'ts' (time series) objects from the stats package, and 'xts' (extensible time-series) objects from the xts package. library(xts) # construct dummy data z <- rnorm(36) # convert it into a mont

Re: [R] Year and month from a sequence

2020-07-14 Thread Jim Lemon
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-s

[R] Year and month from a sequence

2020-07-14 Thread Miluji Sb
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