對於那些感興趣或有類似問題,我已經找出答案,並在下面發佈。注意:代碼中的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")
希望這有助於歡呼
使用'by'用'個月(Ddate)'和'年(Ddate)' –
感謝您的答覆,但我不知道我跟着!抱歉!我比較新 –