从时间序列中删除闰日后,我使用format = %j
来获取一年中的日期DoY
值。然而,最后一个DoY值仍然是366,而不是365,因为DoY
=60被跳过,这就是1996-02-29
所在的位置。从时间序列中删除闰日后,我如何才能获得正确的一年中的哪一天?
类似的StackOverflow问题
示例:
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