- leapyear<-function(x){}
如果那一年可以整除4(比如2004),2月就有29天
如果那一年不能整除100
如果那一年可以整除400
如果x<1,这个function就要return “error”。我不大懂这个条件的意思,可否请大师解释下?
我写的function是这样的:
- install.packages("lubridate")
不知道对不对,但是检验的时候显示ymd找不到。。。。
请各位大师帮帮忙!小妹感激不尽!
- leapyear<- function(dates, years_to_add)
- {
- if(!require(lubridate)) stop("install lubridate")
- if(years_to_add > floor(years_to_add)) stop("error")
-
- new <- dates + years(years_to_add)
- new[
- leap_year(year(dates)) &
- !leap_year(year(dates) + years_to_add) &
- month(dates) == 2 &
- day(dates) == 29
- ] <- NA
- new_dates
- }
- dates <- ymd(c("2004-2-28", "2004-2-29"))