[R] Stacking matrix columns
@vi@e@gross m@iii@g oii gm@ii@com
@vi@e@gross m@iii@g oii gm@ii@com
Sun Aug 6 02:56:06 CEST 2023
Steve,
As Iris pointed out, some implementations of a matrix are actually of a vector with special qualities. There are sometimes choices whether to store it a row at a time or a column at a time.
In R, your data consisted of the integers from 1 to 20 and they clearly are stored a column at a time:
> x<-matrix(1:20,5,4)
>
> x
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20
Your method involved creating a second matrix/ But you could as easily ask for a vector that gives you back the info in that order:
> as.vector(x)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Iris mentioned the fact that the version of built-in matrices you are using in R is actually a vector with an attribute:
> attributes(x)
$dim
[1] 5 4
This means that when you try to work with the matrix and ask for x[3,4] it does a calculation on where in the vector to look and since you want column 4, it means three columns with five things are ahead of it meaning 15 items. Then it sees you want the third item in this fourth column so it adds 3 to make 18 and looks at the 18th item in the vector:
> x<-matrix(data=1:20,nrow=5, ncol=4, byrow=TRUE)
> x
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[4,] 13 14 15 16
[5,] 17 18 19 20
This is now stored differently:
> x[18]
[1] 12
> as.vector(x)
[1] 1 5 9 13 17 2 6 10 14 18 3 7 11 15 19 4 8 12 16 20
And just FYI, you can make multidimensional arrays using the array() function that as far as I know are just extensions of the same analysis with a dim attribute containg the dimensions.
> x[3,4]
[1] 18
> x[18]
[1] 18
> as.vector(x)[18]
[1] 18
The latter two approaches get to look at the pure vector implementation. So your data is already in the order you want and it is just a question of how to access it.
Rather than copying the matrix, if not needed for another purpose, changing the attribute lets you reshape it in many ways, including a 2x10 matrix but also into a vertical or horizontal matrix of 1x20 or 20x1. Of course, if you will be passing the object around to places that expect a vector, it would be safer. Inn this case, it does seem like it becomes seen like any vector.
> length(x)
[1] 20
> str(x)
int [1:5, 1:4] 1 2 3 4 5 6 7 8 9 10 ...
> attr(x, "dim") <- c(length(x), 1)
> dim(x)
[1] 20 1
> str(x)
int [1:20, 1] 1 2 3 4 5 6 7 8 9 10 ...
> typeof(x)
[1] "integer"
But please note my comments above do not apply the same if you make the matric by rows as in:
-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Steven Yen
Sent: Saturday, August 5, 2023 8:11 PM
To: R-help Mailing List <r-help using r-project.org>
Subject: [R] Stacking matrix columns
I wish to stack columns of a matrix into one column. The following
matrix command does it. Any other ways? Thanks.
> x<-matrix(1:20,5,4)
> x
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20
> matrix(x,ncol=1)
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6
[7,] 7
[8,] 8
[9,] 9
[10,] 10
[11,] 11
[12,] 12
[13,] 13
[14,] 14
[15,] 15
[16,] 16
[17,] 17
[18,] 18
[19,] 19
[20,] 20
>
______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list