从 R 中的时间序列中删除闰日后获取一年中的某一天

问题描述

从我的时间序列中删除闰日后,我使用 format = %j 获取年份中的第 DoY 天值。但是,最后一个 DoY 值仍然是 366 而不是 365,因为 DoY = 60 被跳过,这就是 1996-02-29 所在的位置。从我的时间序列中删除闰日后,如何获得正确的年份?

similar StackOverflow question here

示例:

df <- data.frame(matrix(ncol = 2,nrow = 366))
x <- c("date","DoY")
colnames(df) <- x
start = as.Date("1996-01-01")
end = as.Date("1996-12-31")
df$date <- seq.Date(start,end,1)
remove_leap <- as.Date(c("1996-02-29"))
df <- df[!df$date %in% remove_leap,]
df$DoY <- strftime(df$date,format = "%j") #this formats the date to DoY values but still *sees* the leap day giving a max DoY = 366 rather than 365
df$DoY <- as.numeric(df$DoY)

解决方法

我可以从这里取出它并像这样更正 DoY 以使其在 365 处结束:

library(dplyr)
library(lubridate)

df %>% 
  mutate(DoY = day(date),Month = month(date),Year = year(date)) %>% 
  group_by(Year,Month) %>%
  mutate(DoY = DoY - lag(DoY,default = 0)) %>%
  group_by(Year) %>%
  mutate(DoY = cumsum(DoY)) %>% 
  select(-Month) %>%
  slice_tail(n = 10)

# A tibble: 10 x 2
   date         DoY
   <date>     <dbl>
 1 1996-12-22   356
 2 1996-12-23   357
 3 1996-12-24   358
 4 1996-12-25   359
 5 1996-12-26   360
 6 1996-12-27   361
 7 1996-12-28   362
 8 1996-12-29   363
 9 1996-12-30   364
10 1996-12-31   365