[R-Forge #2577] time-based to Date-based index causes 'duplicate' index values #53
Description
Submitted by: Joshua Ulrich
Assigned to: Jeff Ryan
R-Forge link
From an email exchange with Garrett See:
I think going from time-based to date-based is common practice and should be supported. However, I probably don't fully appreciate the problem.
Perhaps a better illustration is to.daily()
which already returns an object indexed by Date, but that object has an indexTZ
:
set.seed(21)
systime <- as.POSIXct('2013-02-05 01:01', tz='America/Chicago')
x <- xts(rnorm(10000), systime-10000:1*60)
(D <- to.daily(x))
indexClass(D) # Date
indexTZ(D) # 'America/Chicago'
D # 02/05 appears twice
Followups:
Date: 2013-02-24 03:38
Sender: Jeff Ryan
Note that this is really an R issue, though one that I would of course like to mask with xts.
> as.Date(format(index(to.period(x,'days'))))
[1] '2013-01-29' '2013-01-30' '2013-01-31' '2013-02-01' '2013-02-02'
[6] '2013-02-03' '2013-02-04' '2013-02-05'
> as.Date((index(to.period(x,'days'))))
[1] '2013-01-30' '2013-01-31' '2013-02-01' '2013-02-02' '2013-02-03'
[6] '2013-02-04' '2013-02-05' '2013-02-05'
.drop.time
should probably use the format()
approach to assure the avoidance of Dates in R as converted from POSIXct
.
Date: 2013-03-02 17:43
Sender: Garrett See
Would it suffice to have to.period
just provide the timezone when converting the index to Date.?
R> as.Date(index(to.period(x, 'days')), origin='1970-01-01', tz='America/Chicago')
[1] "2013-01-29" "2013-01-30" "2013-01-31" "2013-02-01" "2013-02-02"
[6] "2013-02-03" "2013-02-04" "2013-02-05"