[R] No years() function?
Ben Bolker
bolker at zoo.ufl.edu
Wed Mar 7 22:28:03 CET 2007
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.Date
function (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)
More information about the R-help
mailing list