【问题标题】:Counting days of the year with leap years用闰年计算一年中的天数
【发布时间】:2023-03-10 05:55:01
【问题描述】:

我目前正在尝试在 R 中对此进行编码。我想采用 %Y-%m-%d(例如:2017-12-31)格式的日期并将其转换为一年中的某一天。但是,我希望它始终将 02/28 视为第 59 天,将 03/01 视为第 61 天。当它不是闰年时,它会跳过#60。这样,01/01 始终是#1,12/31 始终是#366。

我已经尝试过使用strftime()yday(),但是当它是闰年时,它们都不会跳过第60 天。根据是否是闰年,它只会使 12/31 成为第 365 天或第 366 天。

如果有人对我如何在 R 中编写代码有任何见解,那就太好了!非常感谢。

file <- read.table("PATHTOMYFILE", fill = TRUE, header = TRUE)
file <- file[-c(1), ]
file$datetime <- as.Date(as.character(file$datetime))
file <- file[which(file$datetime <= as.Date("2017-09-30")), ]
file$x <- file[, 4]
file$x <- as.numeric(as.character(file$x))

# Year-day function
yearday <- function(d){
# Count previous months
yd <- ifelse(lubridate::month(d) > 1, sum(lubridate::days_in_month(1: 
(lubridate::month(d)-1))), 0)

# Add days so far in month & extra day if after February
yd <- yd + lubridate::day(d) + ifelse(lubridate::month(d)>2, 1, 0)
yd
}

file$Day <- yearday(as.Date((file$datetime), format = "%Y-%m-%d"))

【问题讨论】:

    标签: r date lubridate strftime leap-year


    【解决方案1】:

    一种选择是使用此功能:

    How to account for leap years?

    leap_year <- function(year) {
      return(ifelse((year %%4 == 0 & year %%100 != 0) | year %%400 == 0, TRUE, FALSE))
    }
    

    然后编写一些代码,根据您的规则(例如闰年)处理您拥有的天数

     if(leap_year(mydate)== TRUE & day_num>60) {
     day_num + 1} else{
      day_num}
    

    【讨论】:

      【解决方案2】:

      您可以使用lubridateleap_year 函数。例如,

      > library(lubridate)
      > 
      > dates <- c(as.Date("2017-12-31"), as.Date("2016-12-31"))
      > 
      > y <- as.Date("2016-12-31")
      > z <- as.Date("2017-12-31")
      > 
      > leap_every_year <- function(x) {
      +   
      +   ifelse(yday(x) > 59 & leap_year(x) == FALSE, yday(x) + 1, yday(x))
      +   
      + }
      > 
      > leap_every_year(y)
      [1] 366
      > leap_every_year(z)
      [1] 366
      > leap_every_year(dates)
      [1] 366 366
      

      编辑:看到这与@MDEWITT 的解决方案非常相似,但它使用 lubridate 代替。类似的想法。祝你好运!

      【讨论】:

      • 很高兴它有帮助!
      【解决方案3】:

      这是一个完成这项工作的函数。它将当前月份之前的所有月份相加,加上当前月份的天数,如果月份在 2 月之后,则加上闰日。

      # Year-day function
      yearday <- function(d){
        # Count previous months
        yd <- ifelse(lubridate::month(d) > 1, sum(lubridate::days_in_month(1:(lubridate::month(d)-1))), 0)
        # Add days so far in month & extra day if after February
        yd <- yd + lubridate::day(d) + ifelse(lubridate::month(d)>2, 1, 0)
        yd
      }
      
      # Test function
      yearday(as.Date("2017-12-31", format = "%Y-%m-%d"))
      #> [1] 366
      yearday(as.Date("2017-03-01", format = "%Y-%m-%d"))
      #> [1] 61
      yearday(as.Date("2017-01-01", format = "%Y-%m-%d"))
      #> [1] 1
      

      reprex package (v0.2.1) 于 2019 年 3 月 1 日创建

      【讨论】:

      • 我在运行您的代码时收到此警告消息。 “警告消息:在 1:(lubridate::month(d) - 1) 中:数值表达式有 29220 个元素:只使用第一个元素”我的开始日期是 1937-10-01,并且计数是正确的,直到它到达 1937- 11-01 并在第 275 天重新开始。
      • @SamanthaSullivan 您能否编辑您的问题以包含您正在运行的数据和确切代码,以便我可以重现该问题?
      • 我添加了代码。我不确定如何添加表格。它刚开始是一个带有 5 个标题的表格。第三个标题称为“日期时间”,其中日期为 $Y-%m-%d 格式,然后我添加了一个名为 x 的第 6 列,它只是我的第 4 列的重命名,这是我正在执行分析的数据值在。如何添加我的数据?
      • @SamanthaSullivan 通过使用dputdump 并复制和粘贴输出来添加数据是最简单的。例如dump("file", ""),如果您的数据框名为file
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-22
      相关资源
      最近更新 更多