[R] Date issues
Marc Schwartz
marc_schwartz at me.com
Sat Sep 18 17:46:05 CEST 2010
On Sep 18, 2010, at 10:25 AM, Santosh Srinivas wrote:
> Strangely this is not working ... what am I doing wrong here?
>
>> tDate <- FnO_Data$Date[1]
>> tDate
> [1] 20090101
>> as.Date(c(tDate),format="%Y%m%d")
> [1] NA
What version of R are you running?
What is the output of:
str(FnO_Data$Date)
and
str(tDate)
You likely need to get rid of the c(). Presumably tDate is a factor and by using c(), you are stripping the factor class attribute from tDate, resulting in a numeric value being passed to as.Date().
Under R 2.11.1, you would get the following:
tDate <- factor(c("20090101", "20090201", "20090301"))
> as.Date(tDate[1], format = "%Y%m%d")
[1] "2009-01-01"
> c(tDate[1])
[1] 1
> as.Date(c(tDate[1]), format = "%Y%m%d")
Error in as.Date.numeric(c(tDate[1]), format = "%Y%m%d") :
'origin' must be supplied
Alternatively, if the original vector is numeric, you would get the following:
> as.Date(20090101, format = "%Y%m%d")
Error in as.Date.numeric(20090101, format = "%Y%m%d") :
'origin' must be supplied
So something is amiss with what you have copied and pasted above, unless you are perhaps running an older version of R. The change in the behavior of as.Date(), relative to converting numeric arguments given that 'origin' is specified, occurred with R version 2.7.0. So are you running a version of R older than that?
HTH,
Marc Schwartz
More information about the R-help
mailing list