[R] Stacking matrix columns
Eric Berger
er|cjberger @end|ng |rom gm@||@com
Sun Aug 6 09:33:31 CEST 2023
Stacking columns of a matrix is a standard operation in multilinear
algebra, usually written as the operator vec().
I checked to see if there is an R package that deals with multilinear
algebra. I found rTensor, which has a function vec().
So, yet another way to accomplish what you want would be:
> library(rTensor)
> vec(as.tensor(x))
Eric
On Sun, Aug 6, 2023 at 5:05 AM Bert Gunter <bgunter.4567 using gmail.com> wrote:
>
> Or just dim(x) <- NULL.
> (as matrices in base R are just vectors with a dim attribute stored in
> column major order)
>
> ergo:
>
> > x
> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> > x<- 1:20 ## a vector
> > is.matrix(x)
> [1] FALSE
> > dim(x) <- c(5,4)
> > is.matrix(x)
> [1] TRUE
> > attributes(x)
> $dim
> [1] 5 4
>
> > ## in painful and unnecessary detail as dim() should be used instead
> > attr(x, "dim") <- NULL
> > is.matrix(x)
> [1] FALSE
> > x
> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
>
> ## well, you get it...
>
> -- Bert
>
> On Sat, Aug 5, 2023 at 5:21 PM Iris Simmons <ikwsimmo using gmail.com> wrote:
> >
> > You could also do
> >
> > dim(x) <- c(length(x), 1)
> >
> > On Sat, Aug 5, 2023, 20:12 Steven Yen <styen using ntu.edu.tw> wrote:
> >
> > > 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.
> > >
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > 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.
>
> ______________________________________________
> 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