1

I can query an xts time range by using two time strings separated by "/":

library(xts)
set.seed(1234)
a = xts(1:10, as.POSIXlt(1366039619, tz="", origin="1970-01-01") + rnorm(10, 0, 3))
                           [,1]
2013-04-15 11:26:51.962906    4
2013-04-15 11:26:55.378802    1
2013-04-15 11:26:56.329886   10
2013-04-15 11:26:57.275780    7
2013-04-15 11:26:57.306643    9
2013-04-15 11:26:57.360104    8
2013-04-15 11:26:59.832287    2
2013-04-15 11:27:00.287374    5
2013-04-15 11:27:00.518167    6
2013-04-15 11:27:02.253323    3

> a['2013-04-15 11:26:57/2013-04-15 11:26:58']
                           [,1]
2013-04-15 11:26:57.275780    7
2013-04-15 11:26:57.306643    9
2013-04-15 11:26:57.360104    8

How can I run the same range query on a different xts object using the POSIXlt objects index(a[4]) and index(a[7])? Do I have to convert the indexes to strings or there is a faster way using integer values, like the number of secs since the epoch embedded in POSIXlt?

Robert Kubrick
  • 8,413
  • 13
  • 59
  • 91

2 Answers2

0

ISO-style character subsetting by range (i.e. a length 1 vector containing : or /) is very fast. So you should use something like this :

a[paste(index(a)[4],index(a)[7],sep='::')]
                    [,1]
2013-04-15 17:26:57    7
2013-04-15 17:26:57    9
2013-04-15 17:26:57    8
2013-04-15 17:26:59    2
agstudy
  • 119,832
  • 17
  • 199
  • 261
0

a[1:10] would extract the first 10 rows, and you don't even have to tell it which columns you want a[30:32] would give you the 30th through the 32 rows.

user2004820
  • 321
  • 2
  • 11