对于那些感兴趣或有类似问题的人,我已经找到了答案并发布在下面。注意:代码中的 L.Vic 和 L.Kyg 指的是两个湖,其中数据按照上述问题在一个数据框中。
require("lubridate"); require("xts"); require("plyr")
# Convert to xts format and then apply monthly mean upon anomalies
x <- xts(L.Vic[,-1], as.POSIXct(L.Vic[,1], format="%d-%b-%y"))
x <- apply.monthly(x$Anomaly_cm, mean)
y <- xts(L.Kyg[,-1], as.POSIXct(L.Kyg[,1], format="%d-%b-%y"))
y <- apply.monthly(y$Anomaly_cm, mean)
# Converts xts row.names to column format and saves as data frame
L.Vic.ts <- data.frame(date=index(x), coredata(x))
L.Kyg.ts <- data.frame(date=index(y), coredata(y))
# Change to 'yyyy-mm' format, then a POSIXct date-time object, then dec-date
L.Vic.ts$date <- strftime(strptime(L.Vic.ts$date, "%Y-%m-%d"), "%Y-%m")
L.Vic.ts$date <- parse_date_time(L.Vic.ts$date, "ym")
L.Vic.ts$date <- decimal_date(L.Vic.ts$date)
colnames(L.Vic.ts) <- c("Decim_Date", "SWSanom")
L.Kyg.ts$date <- strftime(strptime(L.Kyg.ts$date, "%Y-%m-%d"), "%Y-%m")
L.Kyg.ts$date <- parse_date_time(L.Kyg.ts$date, "ym")
L.Kyg.ts$date <- decimal_date(L.Kyg.ts$date)
colnames(L.Kyg.ts) <- c("Decim_Date", "SWSanom")
希望这有帮助,干杯