[R] Unexpected behavior looping through sequence of dates

Alexandre Sieira alexandre.sieira at gmail.com
Sat Mar 9 18:24:45 CET 2013


I understand that the two following loops should produce the exact same output. However, they do not. It appears that looping directly through the sequence of Date objects somehow makes them be coerced to numeric:

> date1 = "20130301"
> date2 = "20130302"
> 
> d1 = as.Date(date1, format="%Y%m%d", tz="GMT")
> d2 = as.Date(date2, format="%Y%m%d", tz="GMT")
> 
> range = seq(from=d1, to=d2, by="day")
> for (i in 1:length(range)) print(class(range[i]))

[1] "Date"
[1] "Date"
> 
> for (i in seq(from=d1, to=d2, by="day")) print(class(i))
[1] "numeric"
[1] "numeric"

This is output from R version 2.15.2 (2012-10-26) on Mac OS X 10.8.2.

Is this expected behavior?

-- 
Alexandre Sieira
CISA, CISSP, ISO 27001 Lead Auditor

"The truth is rarely pure and never simple."
Oscar Wilde, The Importance of Being Earnest, 1895, Act I


Sent with Sparrow (http://www.sparrowmailapp.com/?sig)



More information about the R-help mailing list