I’ve looked through a number of posts with similar titles but none seem to help me out with this code.
I have a very large array (1200 rows, 48 columns, 6 sheets). I’d like to flatten it into a matrix.
So a smaller version to work with:
nchanges <- 4
ncat <- 3
npatches <- 6
A <- array(c(1, -1, 0, 0, 0, 1, -1, 0, 0, 0, 1, -1),
dim=c(nchanges, ncat, npatches))
colnames(A) <- c("S", "I", "R")
and it comes out looking like this (although six slices of it)
, , 1
S I R
[1,] 1 0 0
[2,] -1 1 0
[3,] 0 -1 1
[4,] 0 0 -1
I would like to condense it into one slice to end up with a matrix which looks similar to this:
S1 S2 S3 S4 S5 S6 I1 I2 I3 I4 I5 I6 R1 R2 R3 R4 R5 R6
[1,] 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[2,] 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[3,] 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[4,] 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[5,] 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
[6,] 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
[7,] -1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
[8,] 0 -1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
[9,] 0 0 -1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
[10,] 0 0 0 -1 0 0 0 0 0 1 0 0 0 0 0 0 0 0
[11,] 0 0 0 0 -1 0 0 0 0 0 1 0 0 0 0 0 0 0
[12,] 0 0 0 0 0 -1 0 0 0 0 0 1 0 0 0 0 0 0
...
nrow = npatches*nchanges
I can figure out how to do it with for loops, but I’m sure there is a significantly more computationally smart and efficient way to do this.