library("broadcast")
x <- setNames(1:27, c(letters, NA))
print(x)
## a b c d e f g h i j k l m n o p
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
## q r s t u v w x y z <NA>
## 17 18 19 20 21 22 23 24 25 26 27
y <- vector2array(x, 1L, 3L)
print(y)
## , , 1
##
## [,1]
## a 1
## b 2
## c 3
## d 4
## e 5
## f 6
## g 7
## h 8
## i 9
## j 10
## k 11
## l 12
## m 13
## n 14
## o 15
## p 16
## q 17
## r 18
## s 19
## t 20
## u 21
## v 22
## w 23
## x 24
## y 25
## z 26
## <NA> 27
undim(y)
## a b c d e f g h i j k l m n o p
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
## q r s t u v w x y z <NA>
## 17 18 19 20 21 22 23 24 25 26 27vector2array
Turn Vector to Array and Vice-Versa
Description
vector2array() turns a vector into an array, with a specific vector direction, and turning the names into dimnames, and keeping (or forcing) broadcaster attribute.
undim() returns a copy of an object, but with its dimensions removed, but still trying to keep the names if possible (it somewhat is like the dimensional version of unlist()).
undim() will also keep (or force) the broadcaster attribute
array2vector() is an alias for undim().
Usage
vector2array(x, direction, ndim = direction, broadcaster = NULL)
undim(x, broadcaster = NULL)
array2vector(x, broadcaster = NULL)
Arguments
x
|
an vector (for vector2array() or an array (for undim()/array2vector()). All atomic types, and the recursive type list, are supported.
|
direction
|
a positive integer scalar, giving the direction of the vector. In other words: give here which dimension should have size length(x) - all other dimensions will have size 1.
|
ndim
|
the number of dimensions in total. It must be the case that ndim >= direction, and ndim <= 16L.
|
broadcaster
|
TRUE or FALSE, indicating if the result should be a broadcaster. If NULL, broadcaster(x) will be used. |
Value
For vector2array():
If x is already an array, x is returned unchanged.
Otherwise, given out <- vector2array(x, direction, ndim), out will be an array with the following properties:
-
ndim(out) == ndim; -
dim(out)[direction] == length(x), and all other dimensions will be1; -
dimnames(out)[[direction]] == names(x), and all otherdimnameswill beNULL.
For undim():
If x is not an array, x is returned unchanged.
Otherwise, a copy of the original object, but without dimensions, but keeping names and broadcaster attribute as far as possible.