Change shape and size of a matrix or array.
Usage
resize(x, nrow, ncol, ..., across = c("rows", "columns"), byrow = FALSE)
Arguments
- x
- nrow
The desired number of rows.
- ncol
The desired number of columns.
- ...
Further dimensions of the array.
- across
Character string specifying whether to flatten the matrix across
"rows"
(default) or"columns"
. This option is ignored for multi-way arrays.- byrow
Logical. If
FALSE
(default) the new matrix is filled by columns, otherwise it is filled by rows. This option is ignored for multi-way arrays.
Value
A matrix with dimension nrow
-by-ncol
.
Examples
m <- 1:9
resize(m)
#> [,1]
#> [1,] 1
#> [2,] 2
#> [3,] 3
#> [4,] 4
#> [5,] 5
#> [6,] 6
#> [7,] 7
#> [8,] 8
#> [9,] 9
resize(m, 3, 3)
#> [,1] [,2] [,3]
#> [1,] 1 4 7
#> [2,] 2 5 8
#> [3,] 3 6 9
resize(m, 2, 2)
#> Warning: data length [9] is not a sub-multiple or multiple of the number of rows [2]
#> [,1] [,2]
#> [1,] 1 3
#> [2,] 2 4