[R] Stacking matrix columns
Ebert,Timothy Aaron
tebert @end|ng |rom u||@edu
Mon Aug 7 05:06:14 CEST 2023
You could use a for loop in a brute force approach.
-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Rui Barradas
Sent: Sunday, August 6, 2023 7:37 PM
To: Iris Simmons <ikwsimmo using gmail.com>; Steven Yen <styen using ntu.edu.tw>
Cc: R-help Mailing List <r-help using r-project.org>
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 <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://sta/
>> t.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.e
>> du%7C0777cc9a4f7b4d06730008db96d60c04%7C0d4da0f84a314d76ace60a62331e1
>> b84%7C0%7C0%7C638269618308876684%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w
>> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7
>> C&sdata=SuBbb9Zv2Zodb1p2Urk4a8yl%2FsGNfxUDxB7MqFlaTZc%3D&reserved=0
>> PLEASE do read the posting guide
>> http://www/.
>> r-project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7C
>> 0777cc9a4f7b4d06730008db96d60c04%7C0d4da0f84a314d76ace60a62331e1b84%7
>> C0%7C0%7C638269618308876684%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwM
>> DAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sda
>> ta=usT0%2FPcAyZZsp7IorVV31xXBqlMvH6tO3758UmKja44%3D&reserved=0
>> 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%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu
> %7C0777cc9a4f7b4d06730008db96d60c04%7C0d4da0f84a314d76ace60a62331e1b84
> %7C0%7C0%7C638269618308876684%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw
> MDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sda
> ta=SuBbb9Zv2Zodb1p2Urk4a8yl%2FsGNfxUDxB7MqFlaTZc%3D&reserved=0
> PLEASE do read the posting guide
> http://www.r/
> -project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7C07
> 77cc9a4f7b4d06730008db96d60c04%7C0d4da0f84a314d76ace60a62331e1b84%7C0%
> 7C0%7C638269618308876684%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiL
> CJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=us
> T0%2FPcAyZZsp7IorVV31xXBqlMvH6tO3758UmKja44%3D&reserved=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 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