0

I am trying to run a for loop that loops through a vector of dates. However, the behavior is very strange.:

library(lubridate)

dates<-ymd(c("2014-12-26","2014-12-19"))
for (run_date in dates) { print(run_date)}

As you can see, the dates object looks like the following:

> dates
[1] "2014-12-26 UTC" "2014-12-19 UTC"

However, what is stored in the run_date variable is the following:

> for (run_date in dates) { print(run_date)}
[1] 1419552000
[1] 1418947200

Strange behavior and not quite sure what the issue is. Does anyone have any thoughts?

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
Trexion Kameha
  • 3,362
  • 10
  • 34
  • 60
  • I want to use the "run_date" variable in other places in the code. However, it's not storing as a date. it's storing as 1418947200, which I have no idea what format that is. – Trexion Kameha Jan 20 '15 at 02:15
  • 2
    http://stackoverflow.com/questions/6434663/looping-over-a-date-object-result-in-a-numeric-iterator – Khashaa Jan 20 '15 at 02:19
  • It's the integer value for the `POSIXct` date that you asked it to create with `ymd`. A `str(dates)` will give you `POSIXct[1:2], format: "2014-12-26" "2014-12-19"`. What do you need the for loop to print? – hrbrmstr Jan 20 '15 at 02:20
  • 1
    use `for (i in seq_along(dates)) { print(dates[i])}` – Khashaa Jan 20 '15 at 02:22

0 Answers0