Skip to contents

This helper function creates a data.frame from a given array.

Usage

array2data.frame(x, rows = NULL, cols = NULL)

Arguments

x

Matrix or array.

rows, cols

integer vectors. These two vectors define a partition of the "dimensions" (1,...,n), where n is the number of dimensions of x (i.e. length(dim(x))). If either of the two is missing, then the complement is used. At least one of the arguments "rows" and "cols" has to be given.

Value

data.frame

See also

The helper function array2data.frame is used internally for the plot methods. The function bmatrix (which coerces arrays to matrices) is a simplified version of array2data.frame.

Examples

# test array
x = test_array(dim = c(2,3,2), dimnames = TRUE)
array2data.frame(x, cols = c(1,3,2))
#>   A=1.C=1.B=1 A=2.C=1.B=1 A=1.C=2.B=1 A=2.C=2.B=1 A=1.C=1.B=2 A=2.C=1.B=2
#> 1         111         211         112         212         121         221
#>   A=1.C=2.B=2 A=2.C=2.B=2 A=1.C=1.B=3 A=2.C=1.B=3 A=1.C=2.B=3 A=2.C=2.B=3
#> 1         122         222         131         231         132         232
array2data.frame(x, rows = 1)
#>     A B=1.C=1 B=2.C=1 B=3.C=1 B=1.C=2 B=2.C=2 B=3.C=2
#> 1 A=1     111     121     131     112     122     132
#> 2 A=2     211     221     231     212     222     232
array2data.frame(x, rows = 2:1)
#>     B   A C=1 C=2
#> 1 B=1 A=1 111 112
#> 2 B=2 A=1 121 122
#> 3 B=3 A=1 131 132
#> 4 B=1 A=2 211 212
#> 5 B=2 A=2 221 222
#> 6 B=3 A=2 231 232
array2data.frame(x, rows = c(2,1,3))
#>      B   A   C value
#> 1  B=1 A=1 C=1   111
#> 2  B=2 A=1 C=1   121
#> 3  B=3 A=1 C=1   131
#> 4  B=1 A=2 C=1   211
#> 5  B=2 A=2 C=1   221
#> 6  B=3 A=2 C=1   231
#> 7  B=1 A=1 C=2   112
#> 8  B=2 A=1 C=2   122
#> 9  B=3 A=1 C=2   132
#> 10 B=1 A=2 C=2   212
#> 11 B=2 A=2 C=2   222
#> 12 B=3 A=2 C=2   232

# consider a pseudo socio economic data set
x = test_array(dim = c(2,4,5), random = TRUE)
dimnames(x) = list(sex=c('female','male'),
                   education = c('none','primary','high','university'),
                   age = c('<20','30-40','40-50','50-60','>60'))
array2data.frame(x, cols = 1)
#>     education   age     female        male
#> 1        none   <20  0.2912870 -0.11639284
#> 2     primary   <20  0.2286832 -0.21508529
#> 3        high   <20 -1.9375715  1.57313868
#> 4  university   <20  0.7390487 -0.05978496
#> 5        none 30-40 -1.5768686 -0.03036814
#> 6     primary 30-40 -0.1696321  1.75181919
#> 7        high 30-40 -0.7677395 -0.66016417
#> 8  university 30-40  1.4187800 -0.35884375
#> 9        none 40-50 -0.2217680 -1.18800769
#> 10    primary 40-50  1.5089138 -0.64586635
#> 11       high 40-50 -1.0834977 -0.73025679
#> 12 university 40-50  0.2551248 -1.35018112
#> 13       none 50-60 -0.7411637  0.97666206
#> 14    primary 50-60 -0.7880937  0.58949420
#> 15       high 50-60  0.2074870 -2.26713309
#> 16 university 50-60  0.4030332 -1.91325645
#> 17       none   >60  1.1638880 -0.76630448
#> 18    primary   >60 -0.4986021 -1.12765643
#> 19       high   >60  1.0491673 -1.48171641
#> 20 university   >60 -0.3608579  1.07850521
array2data.frame(x, cols = c(1,2))
#>     age female.none   male.none female.primary male.primary female.high
#> 1   <20   0.2912870 -0.11639284      0.2286832   -0.2150853  -1.9375715
#> 2 30-40  -1.5768686 -0.03036814     -0.1696321    1.7518192  -0.7677395
#> 3 40-50  -0.2217680 -1.18800769      1.5089138   -0.6458664  -1.0834977
#> 4 50-60  -0.7411637  0.97666206     -0.7880937    0.5894942   0.2074870
#> 5   >60   1.1638880 -0.76630448     -0.4986021   -1.1276564   1.0491673
#>    male.high female.university male.university
#> 1  1.5731387         0.7390487     -0.05978496
#> 2 -0.6601642         1.4187800     -0.35884375
#> 3 -0.7302568         0.2551248     -1.35018112
#> 4 -2.2671331         0.4030332     -1.91325645
#> 5 -1.4817164        -0.3608579      1.07850521

# convert an impulse response function into a data.frame
# generate a random statespace model with m=3 outputs, n=2 inputs and s=4 states.
model = stsp(A = matrix(stats::rnorm(16), nrow = 4, ncol = 4), 
             B = matrix(stats::rnorm(8),  nrow = 4, ncol = 2),
             C = matrix(stats::rnorm(12), nrow = 3, ncol = 4), 
             D = matrix(stats::rnorm(6), nrow = 3, ncol = 2))
k = unclass(pseries(model, lag.max = 25))
dimnames(k) = list(y = paste('y[',1:3,']',sep=''), 
                   x = paste('x[',1:2,']',sep=''),
                   lag = 0:25)
head(array2data.frame(k, rows = c(1,3), cols=2))
#>      y lag         x[1]       x[2]
#> 1 y[1]   0 -1.288669115 -0.9719267
#> 2 y[2]   0  0.009571166 -0.4121729
#> 3 y[3]   0 -0.802024722  0.8045345
#> 4 y[1]   1 -2.177071105  0.1324587
#> 5 y[2]   1 -0.366330235  0.2056882
#> 6 y[3]   1  3.433641161  4.1514886