Converting a vector to a 3D matrix
I want this vector
v <- c(111,112,121,122,211,212,221,222)
to convert to a 3D matrix so that the result looks like this:
,,1
111 112
121 122
,,2
211 212
221 222
Usage dim(v) <- c(2,2,2)
results in a structure like
,,1
[,1] [,2]
[1,] 111 121
[2,] 112 122
,,2
[,1] [,2]
[1,] 211 221
[2,] 212 222
I think there is a very simple way to do this, but I am assuming that I am using the wrong keywords on Google. Thank you for your help!
+3
source to share
1 answer
It's hard to understand what you are doing in general, but for this example I can see that after the dim(v) <- c(2,2,2)
resulting array is different from your expected result in transposition / permutation. So i do
aperm(v, c(2,1,3))
That is, we do:
for (i in 1:2) v[,,i] <- t(v[,,i])
+2
source to share