0
通常,任何一年的1月1日都分配給第1天。同樣,任何一年的第1月2月都是第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
我在這裏做錯了什麼?
'if'只需要一個邏輯條件。 'ifelse'被矢量化。那麼四月,五月,六月等呢? – 2013-03-19 08:07:39