Skip to content
Prev 319132 / 398506 Next

Unexpected behavior looping through sequence of dates

On Mar 9, 2013, at 9:24 AM, Alexandre Sieira wrote:

            
It wasn't expected by me either, but it is what both my Mac R 2.15.3 and my Windows(32bit) 2.15.1 display. If you put those dates in a list, you do not get the coercion:
[1] "Date"
[1] "Date"

I was unable to find the reason for the original coercion in the help("for") page or the R Language Definition entry regarding for-loops. On the hunch that coercion via as.vector might be occurring, I tried with with a factor vector and did see that it was coerced to "character" which is the behavior that would be expected if that were happening behind the scenes:

 ff <- factor(letters[1:3])
 as.vector(ff)
#[1] "a" "b" "c"
 for (i in ff) print(class(i))
#[1] "character"
#[1] "character"
#[1] "character"

(I also tried searching for loop in the R Internals document and found nothing that explained this to me.)