Re: [R] Stacking matrix columns

2023-08-06 Thread avi.e.gross
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,]369   12
> unlist(as.list(mymat))
 [1]  1  2  3  4  5  6  7  8  9 10 11 12
  1   2   3   4   5   6   7   8   9  10  11  12 
> as.vector(unlist(as.data.frame(mymat)))
 [1]  1  2  3  4  5  6  7  8  9 10 11 12

But as noted repeatedly, since underneath it all, mymat is a vector with a
dim attribute, the trivial solution is to set that to NULL or make it 1-D so
the internal algorithm works.

Still, if brute force programming as is done in earlier languages is
important to someone, and you do not want to do it in a language like C,
this would work too.


You could use a for loop in a brute force approach. Here is an example of a
function that I think does what you want and accepts not just matrices (2D
only, no arrays) of any kind but also data.frame or tibble objects as well
as row or column vectors..

mat2vec <- function(mat) {
  # Accept a matrix of any type and return it as
  # a vector stacked by columns.
  
  # Do it the dumb way for illustration.
  
  # If fed a data.frame or tibble, convert it to a matrix first.
  # And handle row or column vectors
  if (is.data.frame(mat)) mat <- as.matrix(mat)
  if (is.vector(mat)) mat <- as.matrix(mat)
  
  # Calculate the rows and columns and the typeof
  # to initialize a vector to hold the result.
  rows <- dim(mat)[1L]
  cols <- dim(mat)[2L]
  type <- typeof(mat)
  result <- vector(length=rows*cols, mode=type)
  
  index <- 1
  
  # Double loop to laboriously copy the items to the result
  
  for (col in 1:cols) {
for (row in 1:rows) {
  result[index] <- mat[row, col]
  index <- index + 1
} # end inner loop on rows
  } # end outer loop on cols
  
  return (result)
} # end function daffynition mat2vec

Checking if it works on many cases:

> mymat
 [,1] [,2] [,3] [,4]
[1,]147   10
[2,]258   11
[3,]369   12
> mat2vec(mymat)
 [1]  1  2  3  4  5  6  7  8  9 10 11 12

> mydf
  V1 V2 V3 V4
1  1  4  7 10
2  2  5  8 11
3  3  6  9 12
> mat2vec(mydf)
 [1]  1  2  3  4  5  6  7  8  9 10 11 12

> mytib
# A tibble: 3 × 4
 V1V2V3V4
 
1 1 4 710
2 2 5 811
3 3 6 912
> mat2vec(mytib)
 [1]  1  2  3  4  5  6  7  8  9 10 11 12

> myboolmat <- mymat <= 5
> myboolmat
 [,1]  [,2]  [,3]  [,4]
[1,] TRUE  TRUE FALSE FALSE
[2,] TRUE  TRUE FALSE FALSE
[3,] TRUE FALSE FALSE FALSE
> mat2vec(myboolmat)
 [1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

> mypi <- pi * mymat
> mat2vec(mypi)
 [1]  3.141593  6.283185  9.424778 12.566371 15.707963 18.849556 21.991149
25.132741 28.274334 31.415927
[11] 34.557519 37.699112

> myletters
 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
[1,] "A"  "E"  "I"  "M"  "Q"  "U"  "Y"  "c"  "g"  "k"   "o"   "s"   "w"  
[2,] "B"  "F"  "J"  "N"  "R"  "V"  "Z"  "d"  "h"  "l"   "p"   "t"   "x"  
[3,] "C"  "G"  "K"  "O"  "S"  "W"  "a"  "e"  "i"  "m"   "q"   "u"   "y"  
[4,] "D"  "H"  "L"  "P"  "T"  "X"  "b"  "f"  "j"  "n"   "r"   "v"   "z"  
> mat2vec(myletters)
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R"
"S" "T" "U" "V" "W" "X" "Y" "Z" "a"
[28] "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
"t" "u" "v" "w" "x" "y" "z"

> myNA <- matrix(c(NA, Inf, 0), 4, 6)
> myNA
 [,1] [,2] [,3] [,4] [,5] [,6]
[1,]   NA  Inf0   NA  Inf0
[2,]  Inf0   NA  Inf0   NA
[3,]0   NA  Inf0   NA  Inf
[4,]   NA  Inf0   NA  Inf0
> mat2vec(myNA)
 [1]  NA Inf   0  NA Inf   0  NA Inf   0  NA Inf   0  NA Inf   0
[16]  NA Inf   0  NA Inf   0  NA Inf   0

> myvec <- 1:8
> mat2vec(myvec)
[1] 1 2 3 4 5 6 7 8
> mat2vec(t(myvec))
[1] 1 2 3 4 5 6 7 8

> mylist <- list("lions", "tigers", "bears")
> mylist
[[1]]
[1] "lions"

[[2]]
[1] "tigers"

[[3]]
[1] "bears"

> mat2vec(mylist)
[1] "lions"  "tigers" "bears" 

> myohmy <- list("lions", 1, "tigers", 2, "bears", 3, list("oh", "my"))
> myohmy
[[1]]
[1] "lions"

[[2]]
[1] 1

[[3]]
[1] "tigers"

[[4]]
[1] 2

[[5]]
[1] "bears"

[[6]]
[1] 3

[[7]]
[[7]][[1]]
[1] "oh"

[[7]][[2]]
[1] "my"

> mat2vec(myohmy)
[1] "lions"  "1"  "tigers" "2"  "bears"  "3"  "oh" "my"

Is it bulletproof? Nope. I could make sure it is not any other type or not a
type that cannot be coerced into something like a matrix, or perhaps is of
higher dimensionality.

But as noted, if you already have a valid matrix object, there is a trivial
way to get it by row.


-Original Message-
From: R-help  On Behalf Of Ebert,Timothy Aaron
Sent: Sunday, August 6, 2023 11:06 PM
To: Rui Barradas ; Iris Simmons ;
Steven Yen 
Cc: R-help Mailing List 
Subject: Re: [R] Stacking matrix columns



-Original 

Re: [R] Stacking matrix columns

2023-08-06 Thread Ebert,Timothy Aaron
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, 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] [,2] [,3] [,4]
>> [1,]16   11   16
>> [2,]27   12   17
>> [3,]38   13   18
>> [4,]49   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@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://sta/
>> t.ethz.ch%2Fmailman%2Flistinfo%2Fr-help=05%7C01%7Ctebert%40ufl.e
>> du%7C0777cc9a4f7b4d06730008db96d60c04%7C0d4da0f84a314d76ace60a62331e1
>> b84%7C0%7C0%7C638269618308876684%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w
>> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7
>> C=SuBbb9Zv2Zodb1p2Urk4a8yl%2FsGNfxUDxB7MqFlaTZc%3D=0
>> PLEASE do read the posting guide
>> http://www/.
>> r-project.org%2Fposting-guide.html=05%7C01%7Ctebert%40ufl.edu%7C
>> 0777cc9a4f7b4d06730008db96d60c04%7C0d4da0f84a314d76ace60a62331e1b84%7
>> C0%7C0%7C638269618308876684%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwM
>> DAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
>> ta=usT0%2FPcAyZZsp7IorVV31xXBqlMvH6tO3758UmKja44%3D=0
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>   [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat/
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-help=05%7C01%7Ctebert%40ufl.edu
> %7C0777cc9a4f7b4d06730008db96d60c04%7C0d4da0f84a314d76ace60a62331e1b84
> %7C0%7C0%7C638269618308876684%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw
> MDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> ta=SuBbb9Zv2Zodb1p2Urk4a8yl%2FsGNfxUDxB7MqFlaTZc%3D=0
> PLEASE do read the posting guide
> http://www.r/
> -project.org%2Fposting-guide.html=05%7C01%7Ctebert%40ufl.edu%7C07
> 77cc9a4f7b4d06730008db96d60c04%7C0d4da0f84a314d76ace60a62331e1b84%7C0%
> 7C0%7C638269618308876684%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiL
> CJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=us
> T0%2FPcAyZZsp7IorVV31xXBqlMvH6tO3758UmKja44%3D=0
> and provide commented, minimal, self-contained, reproducible code.
Hello,

Yet another solution.


t(t(c(x)))

or

x |> c() |> t() |> t()


At first I liked it but it's the slowest of the three, OP's, Iris' (the 
fastest).

Hope this helps,

Rui Barradas

__
R-help@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@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.


Re: [R] Stacking matrix columns

2023-08-06 Thread Rui Barradas

À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] [,2] [,3] [,4]
[1,]16   11   16
[2,]27   12   17
[3,]38   13   18
[4,]49   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@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@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.

Hello,

Yet another solution.


t(t(c(x)))

or

x |> c() |> t() |> t()


At first I liked it but it's the slowest of the three, OP's, Iris' (the 
fastest).


Hope this helps,

Rui Barradas

__
R-help@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.


Re: [R] Stacking matrix columns

2023-08-06 Thread avi.e.gross
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.

R has loops and if you loop over some variable moving "col" from 1 to the 
number of columns, then if your matrix is called "mat" you can use mat[,col] to 
grab a whole column at a time and concatenate the results gradually into one 
longer vector.

If you want a more loopy solution, simply reserve a vector big enough to hold 
an M by N matric (size is M*N) and loop over both "row" and "col" and keep 
adding mat[row,col] to your growing vector.

As stated, this is not the way many people think in R, especially those of us 
who know a matrix is just a vector with a bonus.

-Original Message-
From: R-help  On Behalf Of Steven Yen
Sent: Saturday, August 5, 2023 8:08 PM
To: R-help Mailing List 
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,]16   11   16
[2,]27   12   17
[3,]38   13   18
[4,]49   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@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@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.


Re: [R] Stacking matrix columns

2023-08-06 Thread avi.e.gross
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 own way. As you note, it can be 
quite common to do things like flatten a matrix (or higher order array) into a 
vector and do tasks like image recognition using the vector form. 

Mind you, there are other packages that might be a better entry point than 
rTensor and which incorporate that package or similar ones for the user but 
supply other high-level ways to do things like set up neural networks. 

I think the primary focus of this group has been to deal with how to do things 
within a main R distribution and when people start suggesting ways using other 
packages, such as in the tidyverse, that not everyone knows or uses, then there 
is sometimes feedback suggesting a return to more standard topics. Personally, 
I am happy to look at any well-known and appreciated packages even if 
maintained by another company as long as it helps get work done easily. 

I downloaded rTensor just to take a look and see how it implements vec():

> vec
nonstandardGenericFunction for "vec" defined from package "rTensor"

function (tnsr) 
{
standardGeneric("vec")
}

So, no, they do not directly use the trick of setting the dim attribute and 
that may be because they are not operating on a naked matrix/vector but on an 
enhanced version that wraps it as a tensor. 

I experimented with making a small matrix and calling as.tensor() on it and it 
is quite a bit more complex:

> attributes(mymat)
$dim
[1] 3 4

> attributes(mytens)
$num_modes
[1] 2

$modes
[1] 3 4

$data
 [,1] [,2] [,3] [,4]
[1,]147   10
[2,]258   11
[3,]369   12

$class
[1] "Tensor"
attr(,"package")
[1] "rTensor"

This makes sense given that it has to store something more complex. Mind you, 
an R array does it simpler. I am confident this design has advantages for how 
the rTensor package does many other activities. It is more than is needed if 
the OP really has a simple case use.

I do note the vec() function produces the same result as one of the mentioned 
solutions of setting the dim attribute to NULL. 

It would not surprise me if other packages like TensorFlow or ones built on top 
of it like Keras, also have their own ways to do this simple task. The OP may 
want to choose a specific package instead, or as well, for meeting their other 
needs.

-Original Message-
From: Eric Berger  
Sent: Sunday, August 6, 2023 11:59 AM
To: avi.e.gr...@gmail.com
Cc: R-help Mailing List 
Subject: Re: [R] Stacking matrix columns

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.


On Sun, Aug 6, 2023 at 6:16 PM  wrote:
>
> 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 
> structure.
>
> Of course, the OP may have uses in mind which the package might make easier. 
> We often get fairly specific questions here without the additional context 
> that may help guide a better answer.
>
> -Original Message-
> From: R-help  On Behalf Of Eric Berger
> Sent: Sunday, August 6, 2023 3:34 AM
> To: Bert Gunter 
> Cc: R-help Mailing List ; Steven Yen 
> Subject: Re: [R] Stacking matrix columns
>
> 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  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  wrote:
> > >
> > > You could also do
> > 

Re: [R] Stacking matrix columns

2023-08-06 Thread Eric Berger
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.



On Sun, Aug 6, 2023 at 6:16 PM  wrote:
>
> 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 
> structure.
>
> Of course, the OP may have uses in mind which the package might make easier. 
> We often get fairly specific questions here without the additional context 
> that may help guide a better answer.
>
> -Original Message-
> From: R-help  On Behalf Of Eric Berger
> Sent: Sunday, August 6, 2023 3:34 AM
> To: Bert Gunter 
> Cc: R-help Mailing List ; Steven Yen 
> Subject: Re: [R] Stacking matrix columns
>
> 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  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  wrote:
> > >
> > > 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] [,2] [,3] [,4]
> > > > [1,]16   11   16
> > > > [2,]27   12   17
> > > > [3,]38   13   18
> > > > [4,]49   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@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@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@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@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@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.


Re: [R] Stacking matrix columns

2023-08-06 Thread avi.e.gross
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 
structure.

Of course, the OP may have uses in mind which the package might make easier. We 
often get fairly specific questions here without the additional context that 
may help guide a better answer. 

-Original Message-
From: R-help  On Behalf Of Eric Berger
Sent: Sunday, August 6, 2023 3:34 AM
To: Bert Gunter 
Cc: R-help Mailing List ; Steven Yen 
Subject: Re: [R] Stacking matrix columns

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  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  wrote:
> >
> > 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] [,2] [,3] [,4]
> > > [1,]16   11   16
> > > [2,]27   12   17
> > > [3,]38   13   18
> > > [4,]49   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@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@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@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@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@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] Stacking matrix columns

2023-08-06 Thread Steven Yen
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@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.


Re: [R] Stacking matrix columns

2023-08-06 Thread Eric Berger
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  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  wrote:
> >
> > 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] [,2] [,3] [,4]
> > > [1,]16   11   16
> > > [2,]27   12   17
> > > [3,]38   13   18
> > > [4,]49   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@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@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@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@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.