"Date" means "numeric with an attribute of class='Date'
", so what actually happened was that the for loop dropped the class
attribute. In most cases using the seq_along() function lets you step through
index values to extract values from your original vectors. In general, any S3
object will behave this way in a for loop.
for ( i in seq_along( dts ) ) {
print( class( dts[ i ] ) )
}
--
Sent from my phone. Please excuse my brevity.
On November 14, 2017 6:15:03 AM PST, Mikkel Grum <mikkel.grum at
gmail.com> wrote:>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]]
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.