[R] Processing dates and generating sequences of dates

jim holtman jholtman at gmail.com
Tue Feb 5 01:05:11 CET 2008


Does this do what you want?

> your.dates <- c(as.Date('2005-05-21'), as.Date('2007-03-11'))
> x.range <- as.numeric(format(range(your.dates),"%Y"))
> x <- expand.grid(x.range[1]:x.range[2], 1:12)
> x <- sort(sprintf("%d-%02d-01", x[,1], x[,2]))
>
> x
 [1] "2005-01-01" "2005-02-01" "2005-03-01" "2005-04-01" "2005-05-01"
"2005-06-01" "2005-07-01"
 [8] "2005-08-01" "2005-09-01" "2005-10-01" "2005-11-01" "2005-12-01"
"2006-01-01" "2006-02-01"
[15] "2006-03-01" "2006-04-01" "2006-05-01" "2006-06-01" "2006-07-01"
"2006-08-01" "2006-09-01"
[22] "2006-10-01" "2006-11-01" "2006-12-01" "2007-01-01" "2007-02-01"
"2007-03-01" "2007-04-01"
[29] "2007-05-01" "2007-06-01" "2007-07-01" "2007-08-01" "2007-09-01"
"2007-10-01" "2007-11-01"
[36] "2007-12-01"


On Feb 4, 2008 12:09 PM, Gavin Simpson <gavin.simpson at ucl.ac.uk> wrote:
> hits=-2.6 tests=BAYES_00
> X-USF-Spam-Flag: NO
>
> On Mon, 2008-02-04 at 10:48 -0500, Gabor Grothendieck wrote:
> > Using zoo's yearmon class:
> >
> > library(zoo)
> > my.dates[!duplicated(as.yearmon(my.dates))]
> >
> > or, although you seem to disallow this in your question,
> > this would be an option:
> >
> > my.dates[!duplicated(format(my.dates, "%Y-%m"))]
>
> Ah, actually, I spoke too soon. Your solutions return the following
>
> > my.dates[!duplicated(format(my.dates, "%Y-%m"))]
>  [1] "2005-05-01" "2005-06-01" "2005-07-01" "2005-08-01" "2005-09-01"
>  [6] "2005-10-01" "2005-11-01" "2005-12-01" "2006-01-01" "2006-02-01"
> [11] "2006-03-01" "2006-04-01" "2006-05-01" "2006-06-01" "2006-07-01"
> [16] "2006-08-01" "2006-09-01" "2006-10-01" "2006-11-01" "2006-12-01"
> [21] "2007-01-01" "2007-02-01" "2007-03-01" "2007-04-01" "2007-05-01"
> [26] "2007-06-01" "2007-07-01"
>
> which gives only the months sampled. What I need is a vector of length
> 36 covering 1st Jan 2005 through 31st Dec 2007 as in (using the seq()
> call in my original email):
>
> > new.dates
>  [1] "2005-01-01" "2005-02-01" "2005-03-01" "2005-04-01" "2005-05-01"
>  [6] "2005-06-01" "2005-07-01" "2005-08-01" "2005-09-01" "2005-10-01"
> [11] "2005-11-01" "2005-12-01" "2006-01-01" "2006-02-01" "2006-03-01"
> [16] "2006-04-01" "2006-05-01" "2006-06-01" "2006-07-01" "2006-08-01"
> [21] "2006-09-01" "2006-10-01" "2006-11-01" "2006-12-01" "2007-01-01"
> [26] "2007-02-01" "2007-03-01" "2007-04-01" "2007-05-01" "2007-06-01"
> [31] "2007-07-01" "2007-08-01" "2007-09-01" "2007-10-01" "2007-11-01"
> [36] "2007-12-01"
>
> This just seems a bit kludgy:
>
> new.dates <- seq(as.Date(paste(format(min(my.dates), format = "%Y"),
>                               "1", "1", sep = "/")),
>                 as.Date(paste(format(max(my.dates), format = "%Y"),
>                               "12", "31", sep = "/")),
>                 by = "months")
>
> but perhaps there isn't a better way?
>
> Cheers,
>
> G
>
> >
> >
> > On Feb 4, 2008 10:39 AM, Gavin Simpson <gavin.simpson at ucl.ac.uk> wrote:
> > > hits=-2.6 tests=BAYES_00
> > > X-USF-Spam-Flag: NO
> > >
> > > Dear List,
> > >
> > > Say I have the following sequence of dates [*]:
> > >
> > > start <- as.Date("2005-01-05", format = "%Y-%d-%m")
> > > end <- as.Date("2007-10-07", format = "%Y-%d-%m")
> > > my.dates <- seq(start, end, by = "days")
> > >
> > > What I would like to generate is a sequence of dates, by month that goes
> > > from the first day of 2005 (the year of the start date) to the last day
> > > of 2007 (the year of the end date), so that the output is a vector of 36
> > > dates containing all months of the three calendar years that the
> > > sampling spanned.
> > >
> > > I could do it via manipulation of dates as so:
> > >
> > > new.dates <- seq(as.Date(paste(format(min(my.dates), format = "%Y"),
> > >                               "1", "1", sep = "/")),
> > >                 as.Date(paste(format(max(my.dates), format = "%Y"),
> > >                               "12", "31", sep = "/")),
> > >                 by = "months")
> > >
> > > And then manipulate that to get only the month and year parts of the 36
> > > generated dates.
> > >
> > > This doesn't seem very elegant to me. Is there a better/easier way than
> > > converting back and forth between characters and objects of class
> > > "Date"?
> > >
> > > Many thanks,
> > >
> > > G
> > >
> > > [*] FWIW, my actual application is similar to my.dates, but not sampled
> > > every day - indeed there are months where there are no samples - and I
> > > am trying to do a levelplot of the numbers of observations per month,
> > > per year. Given the following data
> > >
> > > dat <- data.frame(temp = rnorm(length(my.dates)),
> > >                  my.dates = my.dates)
> > > dat$year <- as.numeric(format(dat$my.dates, format = "%Y"))
> > > dat$month <- format(dat$my.dates, format = "%b")
> > > dat$month <- factor(dat$month, levels = c("Jan","Feb","Mar","Apr",
> > >                                          "May","Jun","Jul","Aug",
> > >                                          "Sep","Oct","Nov","Dec"))
> > >
> > > I can get a table of the number of observations per month per year via
> > >
> > > (obs.yearmon <- with(dat, table(year, month)))
> > >
> > > Which when converted to a vector provides what I need for levelplot()'s
> > > formula method. Now I just need to generate the sequence of 36 months
> > > ("Jan", "Feb" etc) and years to go with it. The matrix method of
> > > levelplot works, but I am having to hard code a lot of details that I
> > > believe the formula method will handle automagically for me.
> > > --
> > > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> > >  Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
> > >  ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
> > >  Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
> > >  Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
> > >  UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
> > > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> > >
> > > ______________________________________________
> > > R-help at r-project.org mailing list
> > > 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
> > 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.
> --
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
>  Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
>  ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
>  Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
>  Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
>  UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list