Reputation: 1403
I am either misunderstanding the docs or having a problem with the drop.time=TRUE
parameter in to.weekly()
. For a simple example, add a time component onto some sample daily data and roll it up to weekly:
library(xts)
data(sample_matrix)
d <- as.xts(sample_matrix)
index(d) <- index(d)+50
w1 <- to.weekly(d, drop.time=TRUE)
head(w1,1)
d.Open d.High d.Low d.Close
2007-01-07 00:00:50 50.03978 50.42188 49.95041 49.99185
w2 <- to.weekly(d, drop.time=FALSE)
head(w2,1)
d.Open d.High d.Low d.Close
2007-01-07 00:00:50 50.03978 50.42188 49.95041 49.99185
The docs say:
Setting
drop.time
to TRUE (the default) will convert a series that includes a time component into one with just a date index, as the time index is often of little value in lower frequency series.
This question mentions that drop.time
depends upon indexClass(d)[1] == 'POSIXt'
but that appears not to help:
indexClass(d)
[1] "POSIXct" "POSIXt"
indexClass(d) <- c('POSIXt', 'POSIXct')
w3 <- to.weekly(d, drop.time=TRUE, name=NULL)
head(w3,1)
Open High Low Close
2007-01-07 00:00:50 50.03978 50.42188 49.95041 49.99185
I'm sure I can just truncate off the time component, but am curious what I'm doing wrong.
Upvotes: 1
Views: 215
Reputation: 176648
This has been fixed in r613 on R-forge. xts:::.drop.time
was looking for the virtual POSIXt
class in the first position of the class attribute, but (at some point) the order of the POSIXct
and POSIXt
classes had been switched in base R. I tried to make this check more robust to future changes.
I also made xts:::.drop.time
actually change the underlying index. It had been setting indexClass(x) <- "Date"
, but the underlying index was still the original POSIXt
times (including intra-day components). This would cause funky merges.
library(xts)
data(sample_matrix)
d1 <- d2 <- as.xts(sample_matrix)
index(d1) <- index(d1)+50
D1 <- to.daily(d1)
D2 <- to.daily(d2)
head(merge(D1,D2),2) # old behavior
d1.Open d1.High d1.Low d1.Close d2.Open d2.High d2.Low d2.Close
2007-01-02 NA NA NA NA 50.03978 50.11778 49.95041 50.11778
2007-01-02 50.03978 50.11778 49.95041 50.11778 NA NA NA NA
head(merge(D1,D2),1) # new behavior
d1.Open d1.High d1.Low d1.Close d2.Open d2.High d2.Low d2.Close
2007-01-02 50.03978 50.11778 49.95041 50.11778 50.03978 50.11778 49.95041 50.11778
Thanks for the report and example!
Upvotes: 1