1

Possible Duplicate:
R - why does looping over a Date object result in a numeric iterator?

In R I have a list of POSIXct datetimes:

dates <- seq(as.POSIXct("2004-01-01"), as.POSIXct("2004-01-02"), as.difftime(6, units="hours"))
print(dates)
[1] "2004-01-01 00:00:00 EST" "2004-01-01 06:00:00 EST"
[3] "2004-01-01 12:00:00 EST" "2004-01-01 18:00:00 EST"
[5] "2004-01-02 00:00:00 EST"

I want to loop over every date. I can construct the loop as follows:

for(i in 1:length(dates)) { print(dates[i]) }
[1] "2004-01-01 EST"
[1] "2004-01-01 06:00:00 EST"
[1] "2004-01-01 12:00:00 EST"
[1] "2004-01-01 18:00:00 EST"
[1] "2004-01-02 EST"

I would think it's nicer to write the loop as follows:

for(d in dates) { print(d) }
[1] 1072933200
[1] 1072954800
[1] 1072976400
[1] 1072998000
[1] 1073019600

But now it has coerced my POSIXct dates into numerics. Why and how did this happen??

Thanks in advance!

Community
  • 1
  • 1
nfomon
  • 19
  • 1

0 Answers0