Skip to contents

Methods for '[[' in package 'lagged'.

Methods

Indexing with "[[" returns the value for the specified lag. The index should be a single number.

This is the recommended way to extract the value at a single index.

signature(x = "FlexibleLagged", i = "ANY", j = "ANY")

signature(x = "Lagged", i = "numeric", j = "missing")

signature(x = "Lagged2d", i = "numeric", j = "logical")

signature(x = "Lagged2d", i = "numeric", j = "numeric")

signature(x = "slMatrix", i = "numeric", j = "ANY")

signature(x = "Lagged2d", i = "missing", j = "numeric")

signature(x = "Lagged2d", i = "numeric", j = "missing")

signature(x = "FlexibleLagged", i = "missing", j = "numeric")

signature(x = "FlexibleLagged", i = "numeric", j = "missing")

Examples

## for 1d objects the difference of '[' and '[[' is not visible
(acv1 <- acf2Lagged(acf(ldeaths, plot = FALSE)))
#> An object of class "Lagged1d"
#> Slot *data*: 
#>        Lag_0        Lag_1        Lag_2        Lag_3        Lag_4        Lag_5 
#>  1.000000000  0.755051141  0.396956836  0.019395714 -0.355897989 -0.608566374 
#>        Lag_6        Lag_7        Lag_8        Lag_9       Lag_10       Lag_11 
#> -0.681383469 -0.607909875 -0.378212377 -0.012975866  0.383252644  0.650206704 
#>       Lag_12       Lag_13       Lag_14       Lag_15       Lag_16       Lag_17 
#>  0.723167071  0.638001465  0.371577811  0.009467461 -0.293699737 -0.496742216 
#>       Lag_18 
#> -0.585558984 
acv1[1]
#> [1] 0.7550511
acv1[[1]]
#> [1] 0.7550511

## in higher dimenions it matters
acv2 <- acf2Lagged(acf(ts.union(mdeaths, fdeaths), plot = FALSE))
acv2[0]   # array
#> , , 1
#> 
#>           [,1]      [,2]
#> [1,] 1.0000000 0.9762413
#> [2,] 0.9762413 1.0000000
#> 
acv2[[0]] # matrix
#>           [,1]      [,2]
#> [1,] 1.0000000 0.9762413
#> [2,] 0.9762413 1.0000000

## as in standard R conventions, '[' can select arbitrary number of elements
acv2[0:1]
#> , , 1
#> 
#>           [,1]      [,2]
#> [1,] 1.0000000 0.9762413
#> [2,] 0.9762413 1.0000000
#> 
#> , , 2
#> 
#>           [,1]      [,2]
#> [1,] 0.7570591 0.7356685
#> [2,] 0.7443093 0.7295201
#> 
## ... while '[[' selects only one, so this is an error:
if (FALSE) {
acv2[[0:1]]
}

## Lagged2
m <- matrix(10:49, nrow = 4, byrow = TRUE)
m_lagged <- Lagged(m)
m_lagged
#> An object of class "Lagged2d"
#> Slot *data*: 
#>  Lag_0 Lag_1 Lag_2 Lag_3 Lag_4 Lag_5 Lag_6 Lag_7 Lag_8 Lag_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    43    44    45    46    47    48    49

## one index: lag
m_lagged[0:1]
#>      [,1] [,2]
#> [1,]   10   11
#> [2,]   20   21
#> [3,]   30   31
#> [4,]   40   41
m_lagged[0]   # column vector
#>      [,1]
#> [1,]   10
#> [2,]   20
#> [3,]   30
#> [4,]   40
m_lagged[[0]] # numeric
#> [1] 10 20 30 40
## two indices: first is row, second is lag
m_lagged[1 , 0]     # '[' doesn't drop dimensions
#>      [,1]
#> [1,]   10
m_lagged[1 , 0:3]
#>      [,1] [,2] [,3] [,4]
#> [1,]   10   11   12   13

m_lagged[[1 , 0]]   # '[[' does drop dimensions
#> [1] 10
m_lagged[[1 , 0:3]]
#> [1] 10 11 12 13
m_lagged[[1, TRUE]] # the whole first row, as numeric
#>  [1] 10 11 12 13 14 15 16 17 18 19

m_lagged[1:2 , 0:3] # ok, a matrix
#>      [,1] [,2] [,3] [,4]
#> [1,]   10   11   12   13
#> [2,]   20   21   22   23
## ... but the first arg. of '[[' must be of length one,
## so this throws error:
if (FALSE) {
m_lagged[[1:2 , 0:3]]
}