1)列。我們可以使用lubridate的year
/month
/day
或節段性month.day.year
:
1A)通過代上通過lubridate 列
1B)列
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)總 。但是,我們並不需要首先創建列。我們可以只使用aggregate.zoo
直接與原動物園對象,z
,使用lubridate或克隆氏病或取決於它是什麼,你想要做的只是用yearmon
從動物園:
2A)骨料使用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)中骨料使用克隆氏病
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)骨料使用yearmon
如果我們希望每年/月均月月總結,而不是結塊所有一月月在一起,在一起,等於是我們既不需要代上也不lubridate而是可以使用動物園的yearmon
:
library(zoo)
z <- zoo(1:1000, as.Date("1932-01-01") + 0:999)
aggregate(z, yearmon, mean)
感謝,但它不工作:'錯誤format.default(結構(as.character(X),名稱=名稱(X),昏暗朦朧=(X): 無效 '修剪'參數' – sbg
看起來你的日期是因素。你可以使用str()函數來確保你的日期實際上是日期。 –