Unclassy behavior

You’re never too experienced to get caught out by one of R’s quirks. Today: dates! Creating a sequence of dates is easy. Looping over a sequence of dates is also easy, right?

datelist <- seq(as.Date("2020-01-01"), as.Date("2020-01-05"), by="day")

for(thisdate in datelist) {
    print(thisdate)
}

But, no.

[1] 18262
[1] 18263
[1] 18264
[1] 18265
[1] 18266

That is not so helpful, especially if you are actually trying to do something with those dates.

It turns out that R loses the class information on loop indices, so the Date class gets converted back to integer (days since 1970-01-01).

Two potential solutions: convert the integer back into a date, or loop over the index rather than the vector of dates directly.]

# option 1

for(thisdate in datelist) {
print(as.Date(thisdate, origin = "1970-01-01"))
}

[1] "2020-01-01"
[1] "2020-01-02"
[1] "2020-01-03"
[1] "2020-01-04"
[1] "2020-01-05"

# option 2

for(i in seq_along(datelist)) {
thisdate <- datelist[i]
print(thisdate)
}
[1] "2020-01-01"
[1] "2020-01-02"
[1] "2020-01-03"
[1] "2020-01-04"
[1] "2020-01-05"

Both solutions work; neither is quite as elegant as a direct loop.

While I got caught out by dates in a for-loop, this can occur with other classed objects.

Incidentally, I also stumbled on another unexpected occurrence.

thisdate <- datelist[1]

print(thisdate)
[1] "2020-01-01"
cat(thisdate, "\n")
18262

If you use cat() as a way to avoid the line numbers, or within code to print to screen, you may also lose the class information.

This entry was posted in . Bookmark the permalink.