【问题标题】:How to create sequence of date for different calendars(365 days) in R?如何在 R 中为不同的日历(365 天)创建日期序列?
【发布时间】:2017-01-31 07:22:40
【问题描述】:

我有 .nc 数据文件,其中包含单个列中完整时期的每日温度数据。时间变量有 365 天日历(无闰年)。我想计算每月/每年的平均值/最大值/最小值。如何将 as.date 用于 365 天日历?

d<- seq(as.Date("2071-01-01"), as.Date("2099-12-31"),1)

上面的代码创建了序列,但它也包括闰年。

【问题讨论】:

  • 我认为您需要从“d”中提取“年份”,然后用作分组变量。即library(dplyr);data.frame(d) %&gt;% group_by(year = format(d, "%Y")) %&gt;% summarise(min = min(d), max = max(d))
  • d 是日期序列而不是数据序列。我想使用“d”计算每年数据的最小/最大值。
  • 我不知道你有什么,所以,使用format%Y% 的分组,然后得到其他变量的最小值,最大值
  • 什么样的人不会记录或预测 2 月 29 日的温度?

标签: r netcdf


【解决方案1】:

我不确定这是否是最好的方法。我会从序列中删除所有 2 月 29 日。

d <- d[!grepl(x = d, pattern = "-02-29$")]

【讨论】:

    【解决方案2】:

    不确定是要删除所有闰年还是只删除 2 月 29 日

    x<-seq(as.Date("2011-01-01"),as.Date("2016-03-01"),by="day")
    is.leapyear=function(year) return(((year %% 4 == 0) & (year %% 100 != 0)) | (year %% 400 == 0))
    #remove all dates in leap years
    x[!is.leapyear(as.integer(format(x, "%Y")))]
    
    #remove all feb 29s
    x[format(x,"%m-%d") != "02-29"]
    

    【讨论】:

    • 你好 waterling,在第二行 "(year %% 100 != 0)" ,这有什么需要?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多