通常、任意の年の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
私はここで何が間違っているのですか?