【问题标题】:How to find the monthly mean of decimal-date data in R如何在R中找到十进制日期数据的月平均值
【发布时间】:2014-07-08 15:02:33
【问题描述】:

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

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

我很困!提前致谢

【问题讨论】:

  • bymonth(Ddate)year(Ddate)一起使用
  • 感谢您的回复,但我不确定我是否关注!对不起!我比较新

标签: r lubridate


【解决方案1】:

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

希望对你有帮助,干杯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 2021-10-13
    • 2014-10-08
    • 2019-07-17
    • 2023-03-26
    相关资源
    最近更新 更多