通常,将任何一年的 1 月 1 日分配给第 1 天。同样,任何一年的 2 月 1 日都是第 32 天。我想将任何一年的 10 月 1 日分配给第 1 天。我有一个函数做这个:
dayNumber <- function(date){
library(lubridate)
date <- as.Date(date)
if(month(date)==10 | month(date)==11 | month(date)==12)
{x <- yday(date) - 273
return(x)}
if(month(date)==1 | month(date)==2 | month(date)==3)
{y <- yday(date) + 91
return(y)}
}
该功能似乎适用于单个日期:
dayNumber("2002-10-01")
[1] 1
dayNumber("2013-01-01")
[1] 92
但是,当应用于日期向量时,我会收到警告,并且日期编号未正确分配给所有日期:
myDates <- c("2003-11-16", "2007-11-01", "1992-10-11", "1993-11-14", "1995-11-12",
"2002-12-08", "2004-01-25", "2004-12-01", "2002-02-14", "2011-01-21")
dayNumber(myDates)
[1] 47 32 12 45 43 69 -248 63 -228 -252
Warning message:
In if (month(date) == 10 | month(date) == 11 | month(date) == 12) { :
the condition has length > 1 and only the first element will be used
我在这里做错了什么?