Hi, I'm trying to aggregate date values using the aggregate function. For example: aggregate(data,by=list(weekdays(LM),months(LM)),FUN=length) I would also like to aggregate by year but there seems to be no years() function. Should there be one? Is there any alternative choice? Also, a hours() function would be great. Any tip on this? Thanks in advance! S?rgio Nunes
S?rgio Nunes <snunes <at> gmail.com> writes:> > Hi, > > I'm trying to aggregate date values using the aggregate function. For example: > > aggregate(data,by=list(weekdays(LM),months(LM)),FUN=length) > > I would also like to aggregate by year but there seems to be no > years() function. > Should there be one? Is there any alternative choice? > > Also, a hours() function would be great. Any tip on this? > > Thanks in advance! > S?rgio Nunes >Well, working by analogy with the existing functions, this might work (not messing with setting up an S3 default though):> apropos("weekdays")[1] "weekdays" "weekdays.Date" "weekdays.POSIXt"> weekdays.Datefunction (x, abbreviate = FALSE) format(x, ifelse(abbreviate, "%a", "%A")) <environment: namespace:base> d1 = Sys.time() years <- function(x,abbreviate=FALSE) { as.numeric(format(x, ifelse(abbreviate, "%y", "%Y"))) } hours <- function(x) { as.numeric(format(x,"%H")) } years(d1); hours(d1)
>From the help of weekdays:"Note: Other components such as the day of the month or the year are very easy to compute: just use 'as.POSIXlt' and extract the relevant component." Yet another option: help(package="chron") JR El mi?, 07-03-2007 a las 15:35 +0000, S?rgio Nunes escribi?:> Hi, > > I'm trying to aggregate date values using the aggregate function. For example: > > aggregate(data,by=list(weekdays(LM),months(LM)),FUN=length) > > I would also like to aggregate by year but there seems to be no > years() function. > Should there be one? Is there any alternative choice? > > Also, a hours() function would be great. Any tip on this? > > Thanks in advance! > S?rgio Nunes > > ______________________________________________ > R-help at stat.math.ethz.ch 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.-- Dipl.-Biol. JR Ferrer Paris ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Laboratorio de Biolog?a de Organismos --- Centro de Ecolog?a Instituto Venezolano de Investigaciones Cient?ficas (IVIC) Apdo. 21827, Caracas 1020-A Rep?blica Bolivariana de Venezuela Tel: (+58-212) 504-1452 Fax: (+58-212) 504-1088 email: jferrer at ivic.ve clave-gpg: 2C260A95