1) columnas. Podemos utilizar de lubridate year
/month
/day
o de Chron month.day.year
:
1a) columnas vía lubridate
library(zoo)
z <- zoo(1:1000, as.Date("1932-01-01") + 0:999)
library(lubridate)
tt <- time(z)
zz <- cbind(z, year = year(tt), month = month(tt), day = day(tt))
1b) columnas a través de Chron
library(zoo)
z <- zoo(1:1000, as.Date("1932-01-01") + 0:999)
library(chron)
zz <- with(month.day.year(time(z)), zoo(cbind(z, day, month, year)))
2) agregado . Sin embargo, no necesitamos crear columnas en primer lugar. Sólo podemos utilizar aggregate.zoo
directamente con el objeto zoológico original, z
, utilizando lubridate o Chron o simplemente usando yearmon
del zoológico dependiendo de qué es lo que desea hacer:
2a) agregada usando lubridate
library(zoo)
z <- zoo(1:1000, as.Date("1932-01-01") + 0:999)
library(lubridate)
aggregate(z, day, mean)
aggregate(z, month, mean)
aggregate(z, year, mean)
2b) agregada usando Chron
library(zoo)
z <- zoo(1:1000, as.Date("1932-01-01") + 0:999)
library(chron)
mdy <- month.day.year(time(z))
aggregate(z, mdy$day, mean)
aggregate(z, mdy$month, mean)
aggregate(z, mdy$year, mean)
# or
ct <- as.chron(time(z))
aggregate(z, days(ct), mean)
aggregate(z, months(ct), mean)
aggregate(z, years(ct), mean)
# days(ct) and years(ct) can actually
# be shortened to just days and years within the above context
# (and that would work for months too except they would be out of order)
aggregate(z, days, mean)
aggregate(z, years, mean)
2c) agregada usando yearmon
Si queremos resumir cada año/mes en lugar de agrupar todos los meses de enero juntos, todos los meses de febrero juntos, etc, entonces no necesitamos ni Chron ni lubridate sino que podemos utilizar zoológico de yearmon
:
library(zoo)
z <- zoo(1:1000, as.Date("1932-01-01") + 0:999)
aggregate(z, yearmon, mean)
gracias pero no funciona: 'Problemas con format.default (estructura (as.character (x) =, nombres de nombres (x), dIM = dim (x),: no válido 'recortar' argument' – sbg
Parece que sus fechas fueron factores. Puede usar la función str() para asegurarse de que las fechas sean realmente fechas. –