首页 > 解决方案 > 从日期序列中删除闰日

问题描述

我试图从我的日期序列中删除闰日,但是,我收到了错误。请帮忙。

Dates=as.data.frame(seq(as.Date("1979-01-01"), to=as.Date("2016-12-31"),by="days"))
names(Dates)= "Dates"
Dates$year=as.numeric(format(Dates$Dates, "%Y"))
Dates$month=as.numeric(format(Dates$Dates, "%m"))
Dates$day=as.numeric(format(Dates$Dates, "%d"))

if [(Dates$month == 2 & Dates$day == 29)]
    Dates=Dates[]

没有闰日的日期序列

标签: rdate

解决方案


也许你搞砸了数据框和向量?可以考虑下面的代码和库tidyverselibrary(lubridate)

> Dates=seq(as.Date("1979-01-01"), to=as.Date("2016-12-31"),by="days")
> find_leap = function(x){
+   day(x) == 29 & month(x) == 2 
+ }
> 
> Dates[find_leap(Dates)]
 [1] "1980-02-29" "1984-02-29" "1988-02-29" "1992-02-29" "1996-02-29"
 [6] "2000-02-29" "2004-02-29" "2008-02-29" "2012-02-29" "2016-02-29"

推荐阅读