subset.xts: Extract Subsets of xts Objects

Description Usage Arguments Details Value Note Author(s) References See Also Examples

Description

Details on efficient subsetting of xts objects for maximum performance and compatibility.

Usage

1
2
## S3 method for class 'xts'
x[i, j, drop = FALSE, which.i=FALSE, ...]

Arguments

x

xts object

i

the rows to extract. Numeric, timeBased or ISO-8601 style (see details)

j

the columns to extract, numeric or by name

drop

should dimension be dropped, if possible. See NOTE.

which.i

return the ‘i’ values used for subsetting. No subset will be performed.

...

additional arguments (unused)

Details

One of the primary motivations, and key points of differentiation of the time series class xts, is the ability to subset rows by specifying ISO-8601 compatible range strings. This allows for natural range-based time queries without requiring prior knowledge of the underlying time object used in construction.

When a raw character vector is used for the i subset argument, it is processed as if it was ISO-8601 compliant. This means that it is parsed from left to right, according to the following specification:

CCYYMMDD HH:MM:SS.ss+

A full description will be expanded from a left-specified truncated one.

Additionally, one may specify range-based queries by simply supplying two time descriptions seperated by a forward slash:

CCYYMMDD HH:MM:SS.ss+/CCYYMMDD HH:MM:SS.ss

The algorithm to parse the above is .parseISO8601 from the xts package.

ISO-style subsetting, given a range type query, makes use of a custom binary search mechanism that allows for very fast subsetting as no linear search though the index is required. ISO-style character vectors may be longer than length one, allowing for multiple non-contiguous ranges to be selected in one subsetting call.

If a character vector representing time is used in place of numeric values, ISO-style queries, or timeBased objects, the above parsing will be carried out on each element of the i-vector. This overhead can be very costly. If the character approach is used when no ISO range querying is needed, it is recommended to wrap the ‘i’ character vector with the I() function call, to allow for more efficient internal processing. Alternately converting character vectors to POSIXct objects will provide the most performance efficiency.

As xts uses POSIXct time representations of all user-level index classes internally, the fastest timeBased subsetting will always be from POSIXct objects, regardless of the indexClass of the original object. All non-POSIXct time classes are converted to character first to preserve consistent TZ behavior.

Value

An extraction of the original xts object. If which.i is TRUE, the corresponding integer ‘i’ values used to subset will be returned.

Note

By design, drop=FALSE in the default case. This preserves the basic underlying type of matrix and the dim() to be non-NULL. This is different from both matrix and zoo behavior as R uses drop=TRUE. Explicitly passing drop=TRUE may be required when performing certain matrix operations.

Author(s)

Jeffrey A. Ryan

References

ISO 8601: Date elements and interchange formats - Information interchange - Representation of dates and time http://www.iso.org

See Also

xts, .parseISO8601,

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
x <- xts(1:3, Sys.Date()+1:3)
xx <- cbind(x,x)

# drop=FALSE for xts, differs from zoo and matrix
z <- as.zoo(xx)
z/z[,1]

m <- as.matrix(xx)
m/m[,1]

# this will fail with non-conformable arrays (both retain dim)
tryCatch(
  xx/x[,1], 
  error=function(e) print("need to set drop=TRUE")
)

# correct way
xx/xx[,1,drop=TRUE]

# or less efficiently
xx/drop(xx[,1])
# likewise
xx/coredata(xx)[,1]


x <- xts(1:1000, as.Date("2000-01-01")+1:1000)
y <- xts(1:1000, as.POSIXct(format(as.Date("2000-01-01")+1:1000)))

x.subset <- index(x)[1:20]
x[x.subset] # by original index type
system.time(x[x.subset]) 
x[as.character(x.subset)] # by character string. Beware!
system.time(x[as.character(x.subset)]) # slow!
system.time(x[I(as.character(x.subset))]) # wrapped with I(), faster!

x['200001'] # January 2000
x['1999/2000'] # All of 2000 (note there is no need to use the exact start)
x['1999/200001'] # January 2000 

x['2000/200005'] # 2000-01 to 2000-05
x['2000/2000-04-01'] # through April 01, 2000
y['2000/2000-04-01'] # through April 01, 2000 (using POSIXct series)

Example output

Loading required package: zoo

Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric

           ..1 ..2
2017-10-01   1   1
2017-10-02   1   1
2017-10-03   1   1
           ..1 ..2
2017-10-01   1   1
2017-10-02   1   1
2017-10-03   1   1
[1] "need to set drop=TRUE"
           ..1 ..2
2017-10-01   1   1
2017-10-02   1   1
2017-10-03   1   1
           ..1 ..2
2017-10-01   1   1
2017-10-02   1   1
2017-10-03   1   1
           ..1 ..2
2017-10-01   1   1
2017-10-02   1   1
2017-10-03   1   1
           [,1]
2000-01-02    1
2000-01-03    2
2000-01-04    3
2000-01-05    4
2000-01-06    5
2000-01-07    6
2000-01-08    7
2000-01-09    8
2000-01-10    9
2000-01-11   10
2000-01-12   11
2000-01-13   12
2000-01-14   13
2000-01-15   14
2000-01-16   15
2000-01-17   16
2000-01-18   17
2000-01-19   18
2000-01-20   19
2000-01-21   20
   user  system elapsed 
  0.001   0.000   0.001 
           [,1]
2000-01-02    1
2000-01-03    2
2000-01-04    3
2000-01-05    4
2000-01-06    5
2000-01-07    6
2000-01-08    7
2000-01-09    8
2000-01-10    9
2000-01-11   10
2000-01-12   11
2000-01-13   12
2000-01-14   13
2000-01-15   14
2000-01-16   15
2000-01-17   16
2000-01-18   17
2000-01-19   18
2000-01-20   19
2000-01-21   20
   user  system elapsed 
  0.016   0.000   0.016 
   user  system elapsed 
  0.002   0.000   0.002 
           [,1]
2000-01-02    1
2000-01-03    2
2000-01-04    3
2000-01-05    4
2000-01-06    5
2000-01-07    6
2000-01-08    7
2000-01-09    8
2000-01-10    9
2000-01-11   10
2000-01-12   11
2000-01-13   12
2000-01-14   13
2000-01-15   14
2000-01-16   15
2000-01-17   16
2000-01-18   17
2000-01-19   18
2000-01-20   19
2000-01-21   20
2000-01-22   21
2000-01-23   22
2000-01-24   23
2000-01-25   24
2000-01-26   25
2000-01-27   26
2000-01-28   27
2000-01-29   28
2000-01-30   29
2000-01-31   30
           [,1]
2000-01-02    1
2000-01-03    2
2000-01-04    3
2000-01-05    4
2000-01-06    5
2000-01-07    6
2000-01-08    7
2000-01-09    8
2000-01-10    9
2000-01-11   10
2000-01-12   11
2000-01-13   12
2000-01-14   13
2000-01-15   14
2000-01-16   15
2000-01-17   16
2000-01-18   17
2000-01-19   18
2000-01-20   19
2000-01-21   20
2000-01-22   21
2000-01-23   22
2000-01-24   23
2000-01-25   24
2000-01-26   25
2000-01-27   26
2000-01-28   27
2000-01-29   28
2000-01-30   29
2000-01-31   30
2000-02-01   31
2000-02-02   32
2000-02-03   33
2000-02-04   34
2000-02-05   35
2000-02-06   36
2000-02-07   37
2000-02-08   38
2000-02-09   39
2000-02-10   40
2000-02-11   41
2000-02-12   42
2000-02-13   43
2000-02-14   44
2000-02-15   45
2000-02-16   46
2000-02-17   47
2000-02-18   48
2000-02-19   49
2000-02-20   50
2000-02-21   51
2000-02-22   52
2000-02-23   53
2000-02-24   54
2000-02-25   55
2000-02-26   56
2000-02-27   57
2000-02-28   58
2000-02-29   59
2000-03-01   60
2000-03-02   61
2000-03-03   62
2000-03-04   63
2000-03-05   64
2000-03-06   65
2000-03-07   66
2000-03-08   67
2000-03-09   68
2000-03-10   69
2000-03-11   70
2000-03-12   71
2000-03-13   72
2000-03-14   73
2000-03-15   74
2000-03-16   75
2000-03-17   76
2000-03-18   77
2000-03-19   78
2000-03-20   79
2000-03-21   80
2000-03-22   81
2000-03-23   82
2000-03-24   83
2000-03-25   84
2000-03-26   85
2000-03-27   86
2000-03-28   87
2000-03-29   88
2000-03-30   89
2000-03-31   90
2000-04-01   91
2000-04-02   92
2000-04-03   93
2000-04-04   94
2000-04-05   95
2000-04-06   96
2000-04-07   97
2000-04-08   98
2000-04-09   99
2000-04-10  100
2000-04-11  101
2000-04-12  102
2000-04-13  103
2000-04-14  104
2000-04-15  105
2000-04-16  106
2000-04-17  107
2000-04-18  108
2000-04-19  109
2000-04-20  110
2000-04-21  111
2000-04-22  112
2000-04-23  113
2000-04-24  114
2000-04-25  115
2000-04-26  116
2000-04-27  117
2000-04-28  118
2000-04-29  119
2000-04-30  120
2000-05-01  121
2000-05-02  122
2000-05-03  123
2000-05-04  124
2000-05-05  125
2000-05-06  126
2000-05-07  127
2000-05-08  128
2000-05-09  129
2000-05-10  130
2000-05-11  131
2000-05-12  132
2000-05-13  133
2000-05-14  134
2000-05-15  135
2000-05-16  136
2000-05-17  137
2000-05-18  138
2000-05-19  139
2000-05-20  140
2000-05-21  141
2000-05-22  142
2000-05-23  143
2000-05-24  144
2000-05-25  145
2000-05-26  146
2000-05-27  147
2000-05-28  148
2000-05-29  149
2000-05-30  150
2000-05-31  151
2000-06-01  152
2000-06-02  153
2000-06-03  154
2000-06-04  155
2000-06-05  156
2000-06-06  157
2000-06-07  158
2000-06-08  159
2000-06-09  160
2000-06-10  161
2000-06-11  162
2000-06-12  163
2000-06-13  164
2000-06-14  165
2000-06-15  166
2000-06-16  167
2000-06-17  168
2000-06-18  169
2000-06-19  170
2000-06-20  171
2000-06-21  172
2000-06-22  173
2000-06-23  174
2000-06-24  175
2000-06-25  176
2000-06-26  177
2000-06-27  178
2000-06-28  179
2000-06-29  180
2000-06-30  181
2000-07-01  182
2000-07-02  183
2000-07-03  184
2000-07-04  185
2000-07-05  186
2000-07-06  187
2000-07-07  188
2000-07-08  189
2000-07-09  190
2000-07-10  191
2000-07-11  192
2000-07-12  193
2000-07-13  194
2000-07-14  195
2000-07-15  196
2000-07-16  197
2000-07-17  198
2000-07-18  199
2000-07-19  200
2000-07-20  201
2000-07-21  202
2000-07-22  203
2000-07-23  204
2000-07-24  205
2000-07-25  206
2000-07-26  207
2000-07-27  208
2000-07-28  209
2000-07-29  210
2000-07-30  211
2000-07-31  212
2000-08-01  213
2000-08-02  214
2000-08-03  215
2000-08-04  216
2000-08-05  217
2000-08-06  218
2000-08-07  219
2000-08-08  220
2000-08-09  221
2000-08-10  222
2000-08-11  223
2000-08-12  224
2000-08-13  225
2000-08-14  226
2000-08-15  227
2000-08-16  228
2000-08-17  229
2000-08-18  230
2000-08-19  231
2000-08-20  232
2000-08-21  233
2000-08-22  234
2000-08-23  235
2000-08-24  236
2000-08-25  237
2000-08-26  238
2000-08-27  239
2000-08-28  240
2000-08-29  241
2000-08-30  242
2000-08-31  243
2000-09-01  244
2000-09-02  245
2000-09-03  246
2000-09-04  247
2000-09-05  248
2000-09-06  249
2000-09-07  250
2000-09-08  251
2000-09-09  252
2000-09-10  253
2000-09-11  254
2000-09-12  255
2000-09-13  256
2000-09-14  257
2000-09-15  258
2000-09-16  259
2000-09-17  260
2000-09-18  261
2000-09-19  262
2000-09-20  263
2000-09-21  264
2000-09-22  265
2000-09-23  266
2000-09-24  267
2000-09-25  268
2000-09-26  269
2000-09-27  270
2000-09-28  271
2000-09-29  272
2000-09-30  273
2000-10-01  274
2000-10-02  275
2000-10-03  276
2000-10-04  277
2000-10-05  278
2000-10-06  279
2000-10-07  280
2000-10-08  281
2000-10-09  282
2000-10-10  283
2000-10-11  284
2000-10-12  285
2000-10-13  286
2000-10-14  287
2000-10-15  288
2000-10-16  289
2000-10-17  290
2000-10-18  291
2000-10-19  292
2000-10-20  293
2000-10-21  294
2000-10-22  295
2000-10-23  296
2000-10-24  297
2000-10-25  298
2000-10-26  299
2000-10-27  300
2000-10-28  301
2000-10-29  302
2000-10-30  303
2000-10-31  304
2000-11-01  305
2000-11-02  306
2000-11-03  307
2000-11-04  308
2000-11-05  309
2000-11-06  310
2000-11-07  311
2000-11-08  312
2000-11-09  313
2000-11-10  314
2000-11-11  315
2000-11-12  316
2000-11-13  317
2000-11-14  318
2000-11-15  319
2000-11-16  320
2000-11-17  321
2000-11-18  322
2000-11-19  323
2000-11-20  324
2000-11-21  325
2000-11-22  326
2000-11-23  327
2000-11-24  328
2000-11-25  329
2000-11-26  330
2000-11-27  331
2000-11-28  332
2000-11-29  333
2000-11-30  334
2000-12-01  335
2000-12-02  336
2000-12-03  337
2000-12-04  338
2000-12-05  339
2000-12-06  340
2000-12-07  341
2000-12-08  342
2000-12-09  343
2000-12-10  344
2000-12-11  345
2000-12-12  346
2000-12-13  347
2000-12-14  348
2000-12-15  349
2000-12-16  350
2000-12-17  351
2000-12-18  352
2000-12-19  353
2000-12-20  354
2000-12-21  355
2000-12-22  356
2000-12-23  357
2000-12-24  358
2000-12-25  359
2000-12-26  360
2000-12-27  361
2000-12-28  362
2000-12-29  363
2000-12-30  364
2000-12-31  365
           [,1]
2000-01-02    1
2000-01-03    2
2000-01-04    3
2000-01-05    4
2000-01-06    5
2000-01-07    6
2000-01-08    7
2000-01-09    8
2000-01-10    9
2000-01-11   10
2000-01-12   11
2000-01-13   12
2000-01-14   13
2000-01-15   14
2000-01-16   15
2000-01-17   16
2000-01-18   17
2000-01-19   18
2000-01-20   19
2000-01-21   20
2000-01-22   21
2000-01-23   22
2000-01-24   23
2000-01-25   24
2000-01-26   25
2000-01-27   26
2000-01-28   27
2000-01-29   28
2000-01-30   29
2000-01-31   30
           [,1]
2000-01-02    1
2000-01-03    2
2000-01-04    3
2000-01-05    4
2000-01-06    5
2000-01-07    6
2000-01-08    7
2000-01-09    8
2000-01-10    9
2000-01-11   10
2000-01-12   11
2000-01-13   12
2000-01-14   13
2000-01-15   14
2000-01-16   15
2000-01-17   16
2000-01-18   17
2000-01-19   18
2000-01-20   19
2000-01-21   20
2000-01-22   21
2000-01-23   22
2000-01-24   23
2000-01-25   24
2000-01-26   25
2000-01-27   26
2000-01-28   27
2000-01-29   28
2000-01-30   29
2000-01-31   30
2000-02-01   31
2000-02-02   32
2000-02-03   33
2000-02-04   34
2000-02-05   35
2000-02-06   36
2000-02-07   37
2000-02-08   38
2000-02-09   39
2000-02-10   40
2000-02-11   41
2000-02-12   42
2000-02-13   43
2000-02-14   44
2000-02-15   45
2000-02-16   46
2000-02-17   47
2000-02-18   48
2000-02-19   49
2000-02-20   50
2000-02-21   51
2000-02-22   52
2000-02-23   53
2000-02-24   54
2000-02-25   55
2000-02-26   56
2000-02-27   57
2000-02-28   58
2000-02-29   59
2000-03-01   60
2000-03-02   61
2000-03-03   62
2000-03-04   63
2000-03-05   64
2000-03-06   65
2000-03-07   66
2000-03-08   67
2000-03-09   68
2000-03-10   69
2000-03-11   70
2000-03-12   71
2000-03-13   72
2000-03-14   73
2000-03-15   74
2000-03-16   75
2000-03-17   76
2000-03-18   77
2000-03-19   78
2000-03-20   79
2000-03-21   80
2000-03-22   81
2000-03-23   82
2000-03-24   83
2000-03-25   84
2000-03-26   85
2000-03-27   86
2000-03-28   87
2000-03-29   88
2000-03-30   89
2000-03-31   90
2000-04-01   91
2000-04-02   92
2000-04-03   93
2000-04-04   94
2000-04-05   95
2000-04-06   96
2000-04-07   97
2000-04-08   98
2000-04-09   99
2000-04-10  100
2000-04-11  101
2000-04-12  102
2000-04-13  103
2000-04-14  104
2000-04-15  105
2000-04-16  106
2000-04-17  107
2000-04-18  108
2000-04-19  109
2000-04-20  110
2000-04-21  111
2000-04-22  112
2000-04-23  113
2000-04-24  114
2000-04-25  115
2000-04-26  116
2000-04-27  117
2000-04-28  118
2000-04-29  119
2000-04-30  120
2000-05-01  121
2000-05-02  122
2000-05-03  123
2000-05-04  124
2000-05-05  125
2000-05-06  126
2000-05-07  127
2000-05-08  128
2000-05-09  129
2000-05-10  130
2000-05-11  131
2000-05-12  132
2000-05-13  133
2000-05-14  134
2000-05-15  135
2000-05-16  136
2000-05-17  137
2000-05-18  138
2000-05-19  139
2000-05-20  140
2000-05-21  141
2000-05-22  142
2000-05-23  143
2000-05-24  144
2000-05-25  145
2000-05-26  146
2000-05-27  147
2000-05-28  148
2000-05-29  149
2000-05-30  150
2000-05-31  151
           [,1]
2000-01-02    1
2000-01-03    2
2000-01-04    3
2000-01-05    4
2000-01-06    5
2000-01-07    6
2000-01-08    7
2000-01-09    8
2000-01-10    9
2000-01-11   10
2000-01-12   11
2000-01-13   12
2000-01-14   13
2000-01-15   14
2000-01-16   15
2000-01-17   16
2000-01-18   17
2000-01-19   18
2000-01-20   19
2000-01-21   20
2000-01-22   21
2000-01-23   22
2000-01-24   23
2000-01-25   24
2000-01-26   25
2000-01-27   26
2000-01-28   27
2000-01-29   28
2000-01-30   29
2000-01-31   30
2000-02-01   31
2000-02-02   32
2000-02-03   33
2000-02-04   34
2000-02-05   35
2000-02-06   36
2000-02-07   37
2000-02-08   38
2000-02-09   39
2000-02-10   40
2000-02-11   41
2000-02-12   42
2000-02-13   43
2000-02-14   44
2000-02-15   45
2000-02-16   46
2000-02-17   47
2000-02-18   48
2000-02-19   49
2000-02-20   50
2000-02-21   51
2000-02-22   52
2000-02-23   53
2000-02-24   54
2000-02-25   55
2000-02-26   56
2000-02-27   57
2000-02-28   58
2000-02-29   59
2000-03-01   60
2000-03-02   61
2000-03-03   62
2000-03-04   63
2000-03-05   64
2000-03-06   65
2000-03-07   66
2000-03-08   67
2000-03-09   68
2000-03-10   69
2000-03-11   70
2000-03-12   71
2000-03-13   72
2000-03-14   73
2000-03-15   74
2000-03-16   75
2000-03-17   76
2000-03-18   77
2000-03-19   78
2000-03-20   79
2000-03-21   80
2000-03-22   81
2000-03-23   82
2000-03-24   83
2000-03-25   84
2000-03-26   85
2000-03-27   86
2000-03-28   87
2000-03-29   88
2000-03-30   89
2000-03-31   90
2000-04-01   91
           [,1]
2000-01-02    1
2000-01-03    2
2000-01-04    3
2000-01-05    4
2000-01-06    5
2000-01-07    6
2000-01-08    7
2000-01-09    8
2000-01-10    9
2000-01-11   10
2000-01-12   11
2000-01-13   12
2000-01-14   13
2000-01-15   14
2000-01-16   15
2000-01-17   16
2000-01-18   17
2000-01-19   18
2000-01-20   19
2000-01-21   20
2000-01-22   21
2000-01-23   22
2000-01-24   23
2000-01-25   24
2000-01-26   25
2000-01-27   26
2000-01-28   27
2000-01-29   28
2000-01-30   29
2000-01-31   30
2000-02-01   31
2000-02-02   32
2000-02-03   33
2000-02-04   34
2000-02-05   35
2000-02-06   36
2000-02-07   37
2000-02-08   38
2000-02-09   39
2000-02-10   40
2000-02-11   41
2000-02-12   42
2000-02-13   43
2000-02-14   44
2000-02-15   45
2000-02-16   46
2000-02-17   47
2000-02-18   48
2000-02-19   49
2000-02-20   50
2000-02-21   51
2000-02-22   52
2000-02-23   53
2000-02-24   54
2000-02-25   55
2000-02-26   56
2000-02-27   57
2000-02-28   58
2000-02-29   59
2000-03-01   60
2000-03-02   61
2000-03-03   62
2000-03-04   63
2000-03-05   64
2000-03-06   65
2000-03-07   66
2000-03-08   67
2000-03-09   68
2000-03-10   69
2000-03-11   70
2000-03-12   71
2000-03-13   72
2000-03-14   73
2000-03-15   74
2000-03-16   75
2000-03-17   76
2000-03-18   77
2000-03-19   78
2000-03-20   79
2000-03-21   80
2000-03-22   81
2000-03-23   82
2000-03-24   83
2000-03-25   84
2000-03-26   85
2000-03-27   86
2000-03-28   87
2000-03-29   88
2000-03-30   89
2000-03-31   90
2000-04-01   91

xts documentation built on May 2, 2019, 5:18 p.m.