I am trying to calculate the daily mode of this time series. In the example data below, I would like to see the mode of the windDir.c
column per day.
Don't know how to use the apply.daily()
wrapper given the fact there is no "colMode" argument. So, I tried using a custom function in period.apply()
, but to no avail. The code I tried, along with the dput
follows.
ep <- endpoints(wind.d,'days')
modefunc <- function(x) {
tabresult <- tabulate(x)
themode <- which(tabresult == max(tabresult))
if (sum(tabresult == max(tabresult))>1)
themode <- NA
return(themode)
}
period.apply(wind.d$windDir.c, INDEX=ep, FUN=function(x) mode(x))
Reproducible data:
wind.d <- structure(list(date = structure(c(1280635200, 1280635200, 1280635200,
1280635200, 1280635200, 1280635200, 1280635200, 1280721600, 1280721600,
1280721600, 1280721600, 1280721600, 1280721600, 1280721600, 1280808000,
1280808000, 1280808000, 1280808000, 1280808000, 1280808000), class = c("POSIXct",
"POSIXt"), tzone = ""), windDir.c = structure(c(4L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 6L, 5L, 5L, 4L, 5L, 5L
), .Label = c("15", "45", "75", "105", "135", "165", "195", "225",
"255", "285", "315", "345"), class = "factor")), .Names = c("date",
"windDir.c"), class = "data.frame", row.names = c(NA, -20L))