[R] Processing dates and generating sequences of dates
Gavin Simpson
gavin.simpson at ucl.ac.uk
Tue Feb 5 09:55:55 CET 2008
hits=-2.6 tests=BAYES_00
X-USF-Spam-Flag: NO
Dear Gabor and Jim,
Many thanks for you solutions which all do what I wanted to achieve. Now
I am spoilt for choice :-)
All the best,
G
On Mon, 2008-02-04 at 22:38 -0500, Gabor Grothendieck wrote:
> Here is another solution. It uses only R core
> functions. Note that as.Date(cut(x, "years"))
> gives Jan 1 of x's year where x is of class "Date":
>
> y <- as.Date(cut(range(my.dates), "years")) + c(0, 364)
> seq(y[1], y[2], "months")
>
> On Mon, Feb 4, 2008 at 1:00 PM, Gabor Grothendieck
> <ggrothendieck at gmail.com> wrote:
> > OK. Here are two zoo solutions and one using only core
> > functions.
> >
> > In the first dd is the first of the month of Jan
> > and month of Dec of the first and last year respectively
> > so we apply seq.Dates to that.
> >
> > In the second yr is Jan of the first and Jan of the
> > last year as "yearmon" class and we adjust, apply seq and
> > convert that to Date.
> >
> > In the last we convert to POSIXlt and update the internals
> > of that object finally converting back to Date and using seq.
> >
> > library(zoo)
> > dd <- as.Date(floor(as.yearmon(range(my.dates))) + c(0, 11/12))
> > seq(dd[1], dd[2], by = "month")
> >
> > library(zoo)
> > yr <- floor(as.yearmon(range(my.dates)))
> > as.Date(seq(yr[1], yr[2]+11/12, by = 1/12))
> >
> > dd <- as.POSIXlt(range(my.dates))
> > dd$mon <- c(0, 11)
> > dd$mday <- c(1, 1)
> > dd <- as.Date(dd)
> > seq(dd[1], dd[2], by = "month")
> >
> >
> > On Feb 4, 2008 12:09 PM, Gavin Simpson <gavin.simpson at ucl.ac.uk> wrote:
> > > 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.
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
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
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
More information about the R-help
mailing list