[R] Dates to numeric in for loop

Mikkel Grum mikkel.grum at gmail.com
Tue Nov 14 15:15:03 CET 2017


Hi

Can anyone explain why a date becomes numeric when you loop over a series
of dates?

> dt <- Sys.Date()
> dt
[1] "2017-11-14"
> class(dt)
[1] "Date"
> dts <- dt - 1:0
> class(dts)
[1] "Date"
>
> for (i in dts) {
+     print(i)
+     print(class(i))
+     print(as.Date(i, "1970-01-01"))
+     print(class(as.Date(i, "1970-01-01")))
+ }
[1] 17483
[1] "numeric"
[1] "2017-11-13"
[1] "Date"
[1] 17484
[1] "numeric"
[1] "2017-11-14"
[1] "Date"

Why is this apparently not a bug? Are there other types that change type
when looped over?

Kind regards
Mikkel



_____________________________________________________
*Mikkel Grum*
+44 7377337321 (mobile)
mikkelgrum (Skype)

	[[alternative HTML version deleted]]



More information about the R-help mailing list