This has been frustrating me. Even with lubridate
I can't get dates to maintain their type when I loop over them. For example:
require(lubridate)
yearrange = ymd(20110101) + years(seq(4))
yearrange
#[1] "2012-01-01 UTC" "2013-01-01 UTC" "2014-01-01 UTC" "2015-01-01 UTC"
class(yearrange)
#[1] "POSIXct" "POSIXt"
However, if I try to loop through years (creating a separate plot for each year in my data set): I lose the formatting of the year, and would have to re-cast the data
for (yr in yearrange) { show(yr) }
#[1] 1325376000
#[1] 1356998400
#[1] 1388534400
#[1] 1420070400
If I loop though specifying indices, I get date objects back:
for (i in seq(length(yearrange))) { show(yearrange[i]) }
#[1] "2012-01-01 UTC"
#[1] "2013-01-01 UTC"
#[1] "2014-01-01 UTC"
#[1] "2015-01-01 UTC"
Is there an easy way to avoid the indexed option, without using foreach
, or is that the only way?