[R-SIG-Finance] xts() speed on data with date index
Jeff Ryan
jeff.a.ryan at gmail.com
Sat Jul 25 16:46:43 CEST 2009
An example to clarify/confuse ;)
> system.time(px<-xts(pr,t))
user system elapsed
0.001 0.001 0.002
> system.time(dx<-xts(pr,d))
user system elapsed
0.164 0.004 0.168
> indexClass(dx)
[1] "Date"
> indexClass(px)
[1] "POSIXt" "POSIXct"
## in POSIXct time as expected
> head(px)
[,1] [,2] [,3] [,4]
2009-07-26 09:39:32 0.6739000 0.6124116 -0.5820407 0.1022264
2009-07-27 09:39:32 -0.7806308 -0.4988508 -0.5448239 -0.5649892
2009-07-28 09:39:32 1.0275375 -0.8538655 -0.6098992 1.9661175
2009-07-29 09:39:32 0.6268532 -0.3144514 1.0142005 -0.2303953
2009-07-30 09:39:32 -3.8062200 -0.3875341 -0.9251609 -0.4677076
2009-07-31 09:39:32 -1.1192617 -0.6575085 -0.9918533 -0.8743504
> head(dx)
[,1] [,2] [,3] [,4]
2009-07-26 0.6739000 0.6124116 -0.5820407 0.1022264
2009-07-27 -0.7806308 -0.4988508 -0.5448239 -0.5649892
2009-07-28 1.0275375 -0.8538655 -0.6098992 1.9661175
2009-07-29 0.6268532 -0.3144514 1.0142005 -0.2303953
2009-07-30 -3.8062200 -0.3875341 -0.9251609 -0.4677076
2009-07-31 -1.1192617 -0.6575085 -0.9918533 -0.8743504
> indexClass(px) <- "Date"
### simply convert the indexClass -- this is essentially costless,
### and now you have an xts object 'indexed' by Date
> head(px)
[,1] [,2] [,3] [,4]
2009-07-26 0.6739000 0.6124116 -0.5820407 0.1022264
2009-07-27 -0.7806308 -0.4988508 -0.5448239 -0.5649892
2009-07-28 1.0275375 -0.8538655 -0.6098992 1.9661175
2009-07-29 0.6268532 -0.3144514 1.0142005 -0.2303953
2009-07-30 -3.8062200 -0.3875341 -0.9251609 -0.4677076
2009-07-31 -1.1192617 -0.6575085 -0.9918533 -0.8743504
> system.time(indexClass(px) <- "Date")
user system elapsed
0.001 0.001 0.001
Now convert back, and see if something like merge cares about the
index class... (hint: it doesn't)
> system.time(indexClass(px) <- c("POSIXt","POSIXct"))
user system elapsed
0.001 0.000 0.000
> indexClass(px)
[1] "POSIXt" "POSIXct"
> head(px)
[,1] [,2] [,3] [,4]
2009-07-26 09:39:32 0.6739000 0.6124116 -0.5820407 0.1022264
2009-07-27 09:39:32 -0.7806308 -0.4988508 -0.5448239 -0.5649892
2009-07-28 09:39:32 1.0275375 -0.8538655 -0.6098992 1.9661175
2009-07-29 09:39:32 0.6268532 -0.3144514 1.0142005 -0.2303953
2009-07-30 09:39:32 -3.8062200 -0.3875341 -0.9251609 -0.4677076
2009-07-31 09:39:32 -1.1192617 -0.6575085 -0.9918533 -0.8743504
> head(dx)
[,1] [,2] [,3] [,4]
2009-07-26 0.6739000 0.6124116 -0.5820407 0.1022264
2009-07-27 -0.7806308 -0.4988508 -0.5448239 -0.5649892
2009-07-28 1.0275375 -0.8538655 -0.6098992 1.9661175
2009-07-29 0.6268532 -0.3144514 1.0142005 -0.2303953
2009-07-30 -3.8062200 -0.3875341 -0.9251609 -0.4677076
2009-07-31 -1.1192617 -0.6575085 -0.9918533 -0.8743504
> system.time(merge(dx,dx))
user system elapsed
0.001 0.000 0.002
> system.time(merge(px,px))
user system elapsed
0.001 0.000 0.002
### Caveat: converting back and forth is a bit silly, and possibly
perilous, but it is
### illustrative of what you can do with xts
HTHsomewhat
Jeff
On Sat, Jul 25, 2009 at 7:59 AM, michael li<michaellibeijing at gmail.com> wrote:
> I like xts because subsetting is easier. Something like x["2009-08"].
> But it seems that xts() is a little slower than zoo() when converting
> data with date index instead of time index.
>
>> pr<-matrix(rnorm(20000),nrow=5000,ncol=4)
>> t<-Sys.time()+(1:5000)*24*3600
>> d<-as.Date(t)
>> system.time(x<-zoo(pr,t))
> user system elapsed
> 0 0 0
>> system.time(x<-zoo(pr,d))
> user system elapsed
> 0 0 0
>> system.time(x<-xts(pr,t))
> user system elapsed
> 0 0 0
>> system.time(x<-xts(pr,d))
> user system elapsed
> 0.34 0.00 0.35
>
> Regards,
> Michael
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>
--
Jeffrey Ryan
jeffrey.ryan at insightalgo.com
ia: insight algorithmics
www.insightalgo.com
More information about the R-SIG-Finance
mailing list