1

一如既往的任何帮助表示赞赏。我有湖泊深度的每日数据(每天 2 次观察)。我已经将日期转换为 decimal_date 格式,例如 2000 年 1 月 2 日的 2000.003,但需要创建每月平均异常的时间序列 - 每月一个值。如果有助于可视化,我已经附上了数据框中前几个数据的副本。

Decim_Date  Depth (cm)  Anomaly (cm)
1   2000.000    1216    78.6721
2   2000.000    1216    78.6721
3   2000.003    1216    78.6721
4   2000.003    1217    79.6721
5   2000.005    1216    78.6721
6   2000.005    1216    78.6721
7   2000.008    1215    77.6721
8   2000.008    1216    78.6721
9   2000.011    1215    77.6721
10  2000.011    1216    78.6721

我很困!提前致谢

4

1 回答 1

0

对于那些感兴趣或有类似问题的人,我已经找到了答案并发布在下面。注意:代码中的 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")

希望这有帮助,干杯

于 2014-07-17T13:38:40.710 回答