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:
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
> ma
Eric,
I am not sure your solution is particularly economical albeit it works for
arbitrary arrays of any dimension, presumably. But it seems to involve
converting a matrix to a tensor just to undo it back to a vector. Other
solutions offered here, simply manipulate the dim attribute of the data
Avi,
I was not trying to provide the most economical solution. I was trying
to anticipate that people (either the OP or others searching for how
to stack columns of a matrix) might be motivated by calculations in
multilinear algebra, in which case they might be interested in the
rTensor package.
Eric,
I fully agreed with you that anyone doing serious work in various projects such
as machine learning that make heavy use of mathematical data structures would
do well to find some decent well designed and possibly efficient packages to do
much of the work rather than re-inventing their ow
Based on a private communication, it sounds like Steven is asking the question
again because he wants a different solution that may be the way this might be
done in another language. I think he wants to use loops explicitly and I
suspect this may be along the lines of a homework problem for him.
Às 01:15 de 06/08/2023, Iris Simmons escreveu:
You could also do
dim(x) <- c(length(x), 1)
On Sat, Aug 5, 2023, 20:12 Steven Yen 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
You could use a for loop in a brute force approach.
-Original Message-
From: R-help On Behalf Of Rui Barradas
Sent: Sunday, August 6, 2023 7:37 PM
To: Iris Simmons ; Steven Yen
Cc: R-help Mailing List
Subject: Re: [R] Stacking matrix columns
[External Email]
Às 01:15 de 06/08/2023, Ir
This topic is getting almost funny as there are an indefinite ever-sillier
set of ways to perform the action and even more if you include packages like
purr.
If mymat is a matrix, several variants work such as:
> mymat
[,1] [,2] [,3] [,4]
[1,]147 10
[2,]258 11
[3,
9 matches
Mail list logo