Reshape a vector or array to have 3 dimensions.

Description

Coerce a vector or array to have 3 dimensions, preserving dimnames if feasible. Throw an error if length(dim(x)) > 3.

Usage

1

Arguments

x

A vector or array.

Details

1. dimx <- dim(x); ndim <- length(dimx)

2. if(ndim==3)return(x).

3. if(ndim>3)stop.

4. x2 <- as.matrix(x)

5. dim(x2) <- c(dim(x2), 1)

6. xnames <- dimnames(x)

7. if(is.list(xnames))dimnames(x2) <- list(xnames[[1]], xnames[[2]], NULL)

Value

A 3-dimensional array with names matching x

Author(s)

Spencer Graves

See Also

dim, dimnames checkDims3

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
##
## vector -> array 
##
as.array3(c(a=1, b=2)) 

##
## matrix -> array 
##
as.array3(matrix(1:6, 2))
as.array3(matrix(1:6, 2, dimnames=list(letters[1:2], LETTERS[3:5]))) 

##
## array -> array 
##
as.array3(array(1:6, 1:3)) 

##
## 4-d array 
##
## Not run: 
as.array3(array(1:24, 1:4)) 
Error in as.array3(array(1:24, 1:4)) : 
  length(dim(array(1:24, 1:4)) = 4 > 3

## End(Not run)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.