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]]
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 at 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 at 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.
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 monthly time series starting from January 1861 a <- ts(z,start=c(1861,1),frequency=12) # create an xts object from 'a' b <- as.xts(a) # you can use index(b) to get what you want, e.g. as.Date(index(b)) # [1] "1861-01-01" "1861-02-01" "1861-03-01" "1861-04-01" "1861-05-01" "1861-06-01" # [7] "1861-07-01" "1861-08-01" "1861-09-01" "1861-10-01" "1861-11-01" "1861-12-01" # [13] "1862-01-01" "1862-02-01" "1862-03-01" "1862-04-01" "1862-05-01" "1862-06-01" # [19] "1862-07-01" "1862-08-01" "1862-09-01" "1862-10-01" "1862-11-01" "1862-12-01" # [25] "1863-01-01" "1863-02-01" "1863-03-01" "1863-04-01" "1863-05-01" "1863-06-01" # [31] "1863-07-01" "1863-08-01" "1863-09-01" "1863-10-01" "1863-11-01" "1863-12-01" HTH, Eric On Wed, Jul 15, 2020 at 1:59 AM Jim Lemon <drjimlemon at gmail.com> wrote:> > 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 at 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 at 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. > > ______________________________________________ > R-help at 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.
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 at 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. >
Dear Jim, Eric, and Rui, Great solutions! Thank you so much. Best, Milu On Wed, Jul 15, 2020 at 9:20 AM Rui Barradas <ruipbarradas at sapo.pt> 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 <- 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 at 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. > > >[[alternative HTML version deleted]]