Skip to content
Prev 4649 / 15274 Next

xts() speed on data with date index

An example to clarify/confuse ;)
user  system elapsed
  0.001   0.001   0.002
user  system elapsed
  0.164   0.004   0.168
[1] "Date"
[1] "POSIXt"  "POSIXct"

##  in POSIXct time as expected
[,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
[,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
### simply convert the indexClass -- this is essentially costless,
### and now you have an xts object 'indexed' by Date
[,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
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)
user  system elapsed
  0.001   0.000   0.000
[1] "POSIXt"  "POSIXct"
[,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
[,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
user  system elapsed
  0.001   0.000   0.002
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: