I would like to convert a vector into an array by filling first the lines of the array (instead of filling first the columns), would someone know a trick to do that? Thanks in advance!
I tried with these lines:
v=c(1,2,3,4,5,6)
M=array(data=v,dim=c(2,3))
But I get the following array:
> M
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
While I would like to get
> M
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
CodePudding user response:
You can use matrix
> matrix(c(1,2,3,4,5,6), byrow = TRUE, nrow=2)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
We can use array
aperm
transposing in dim=c(3,2)
> aperm(array(data=v,dim=c(3,2)), c(2,1))
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
Or simply follow @thelatemail's comment