Convert a vector into an array given a set of dimensions; it therefore simply wraps aperm() and array().

vec2tab(vec, dim)

Arguments

vec

A vector

dim

The desired array dimensions, oftentimes a vector of the number of levels of each variable in order

Value

An array

Details

This function converts an array (or a multi-way contingency table) into a vector, using a consistent ordering of the cells. The ordering of the cells is lexicographical and cannot be specified by the user.

See also

Examples

data(Titanic) Titanic
#> , , Age = Child, Survived = No #> #> Sex #> Class Male Female #> 1st 0 0 #> 2nd 0 0 #> 3rd 35 17 #> Crew 0 0 #> #> , , Age = Adult, Survived = No #> #> Sex #> Class Male Female #> 1st 118 4 #> 2nd 154 13 #> 3rd 387 89 #> Crew 670 3 #> #> , , Age = Child, Survived = Yes #> #> Sex #> Class Male Female #> 1st 5 1 #> 2nd 11 13 #> 3rd 13 14 #> Crew 0 0 #> #> , , Age = Adult, Survived = Yes #> #> Sex #> Class Male Female #> 1st 57 140 #> 2nd 14 80 #> 3rd 75 76 #> Crew 192 20 #>
tab2vec(Titanic)
#> 1,1,1,1 1,1,1,2 1,1,2,1 1,1,2,2 1,2,1,1 1,2,1,2 1,2,2,1 1,2,2,2 2,1,1,1 2,1,1,2 #> 0 5 118 57 0 1 4 140 0 11 #> 2,1,2,1 2,1,2,2 2,2,1,1 2,2,1,2 2,2,2,1 2,2,2,2 3,1,1,1 3,1,1,2 3,1,2,1 3,1,2,2 #> 154 14 0 13 13 80 35 13 387 75 #> 3,2,1,1 3,2,1,2 3,2,2,1 3,2,2,2 4,1,1,1 4,1,1,2 4,1,2,1 4,1,2,2 4,2,1,1 4,2,1,2 #> 17 14 89 76 0 0 670 192 0 0 #> 4,2,2,1 4,2,2,2 #> 3 20
vec2tab(tab2vec(Titanic), dim(Titanic))
#> , , 1, 1 #> #> [,1] [,2] #> [1,] 0 0 #> [2,] 0 0 #> [3,] 35 17 #> [4,] 0 0 #> #> , , 2, 1 #> #> [,1] [,2] #> [1,] 118 4 #> [2,] 154 13 #> [3,] 387 89 #> [4,] 670 3 #> #> , , 1, 2 #> #> [,1] [,2] #> [1,] 5 1 #> [2,] 11 13 #> [3,] 13 14 #> [4,] 0 0 #> #> , , 2, 2 #> #> [,1] [,2] #> [1,] 57 140 #> [2,] 14 80 #> [3,] 75 76 #> [4,] 192 20 #>
vec2tab(tab2vec(Titanic), dim(Titanic)) == Titanic
#> , , Age = Child, Survived = No #> #> Sex #> Class Male Female #> 1st TRUE TRUE #> 2nd TRUE TRUE #> 3rd TRUE TRUE #> Crew TRUE TRUE #> #> , , Age = Adult, Survived = No #> #> Sex #> Class Male Female #> 1st TRUE TRUE #> 2nd TRUE TRUE #> 3rd TRUE TRUE #> Crew TRUE TRUE #> #> , , Age = Child, Survived = Yes #> #> Sex #> Class Male Female #> 1st TRUE TRUE #> 2nd TRUE TRUE #> 3rd TRUE TRUE #> Crew TRUE TRUE #> #> , , Age = Adult, Survived = Yes #> #> Sex #> Class Male Female #> 1st TRUE TRUE #> 2nd TRUE TRUE #> 3rd TRUE TRUE #> Crew TRUE TRUE #>
all(vec2tab(tab2vec(Titanic), dim(Titanic)) == Titanic)
#> [1] TRUE