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"))]
Gabor, Many thanks. Both are exactly what I was looking for. My head was stuck very firmly in a rut of using tools that work with "Date" objects and didn't think about using duplicated! The latter is especially fine because: 1. I didn't mean to imply that I didn't want to convert to/from character - just that my doing so repeatedly in the example didn't seem very elegant. 2. This whole thing may end up being wrapped into a function for use by people who have restricted computing environments, so a pure R solution is advantageous. All the best, G > > > On Feb 4, 2008 10:39 AM, Gavin Simpson <[EMAIL PROTECTED]> 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@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@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.