Hello!
Recently I am trying to transfer a large 3-dimensional array to a matrix. For
example, a array like:
, , 1
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
, , 2
[,1] [,2]
[1,] 7 10
[2,] 8 11
[3,] 9 12
, , 3
[,1] [,2]
[1,] 13 16
[2,] 14 17
[3,] 15 18
I would like to transfer it to a matrix like:
1 7 13
4 10 16
2 8 14
5 11 17
3 9 15
6 12 18
Could you tell me how to do it in R ? Thank you very much!
Best regards,
Chunyu
[[alternative HTML version deleted]]
> x <- array(1:18, dim=c(3, 2, 3)) > x, , 1 [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 , , 2 [,1] [,2] [1,] 7 10 [2,] 8 11 [3,] 9 12 , , 3 [,1] [,2] [1,] 13 16 [2,] 14 17 [3,] 15 18> apply(x, 3, t)[,1] [,2] [,3] [1,] 1 7 13 [2,] 4 10 16 [3,] 2 8 14 [4,] 5 11 17 [5,] 3 9 15 [6,] 6 12 18 On Tue, Oct 20, 2015 at 12:39 PM, Chunyu Dong <dongchunyu2004 at 163.com> wrote:> Hello! > > > Recently I am trying to transfer a large 3-dimensional array to a matrix. > For example, a array like: > , , 1 > [,1] [,2] > [1,] 1 4 > [2,] 2 5 > [3,] 3 6 > , , 2 > [,1] [,2] > [1,] 7 10 > [2,] 8 11 > [3,] 9 12 > , , 3 > [,1] [,2] > [1,] 13 16 > [2,] 14 17 > [3,] 15 18 > > > I would like to transfer it to a matrix like: > 1 7 13 > 4 10 16 > 2 8 14 > 5 11 17 > 3 9 15 > 6 12 18 > > > Could you tell me how to do it in R ? Thank you very much! > > > Best regards, > Chunyu > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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]]
Or use aperm() (array index permuation):
> array(aperm(x, c(2,1,3)), c(6,3))
[,1] [,2] [,3]
[1,] 1 7 13
[2,] 4 10 16
[3,] 2 8 14
[4,] 5 11 17
[5,] 3 9 15
[6,] 6 12 18
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Tue, Oct 20, 2015 at 11:31 AM, John Laing <john.laing at gmail.com>
wrote:>> x <- array(1:18, dim=c(3, 2, 3))
>> x
> , , 1
>
> [,1] [,2]
> [1,] 1 4
> [2,] 2 5
> [3,] 3 6
>
> , , 2
>
> [,1] [,2]
> [1,] 7 10
> [2,] 8 11
> [3,] 9 12
>
> , , 3
>
> [,1] [,2]
> [1,] 13 16
> [2,] 14 17
> [3,] 15 18
>
>> apply(x, 3, t)
> [,1] [,2] [,3]
> [1,] 1 7 13
> [2,] 4 10 16
> [3,] 2 8 14
> [4,] 5 11 17
> [5,] 3 9 15
> [6,] 6 12 18
>
>
> On Tue, Oct 20, 2015 at 12:39 PM, Chunyu Dong <dongchunyu2004 at
163.com>
> wrote:
>
>> Hello!
>>
>>
>> Recently I am trying to transfer a large 3-dimensional array to a
matrix.
>> For example, a array like:
>> , , 1
>> [,1] [,2]
>> [1,] 1 4
>> [2,] 2 5
>> [3,] 3 6
>> , , 2
>> [,1] [,2]
>> [1,] 7 10
>> [2,] 8 11
>> [3,] 9 12
>> , , 3
>> [,1] [,2]
>> [1,] 13 16
>> [2,] 14 17
>> [3,] 15 18
>>
>>
>> I would like to transfer it to a matrix like:
>> 1 7 13
>> 4 10 16
>> 2 8 14
>> 5 11 17
>> 3 9 15
>> 6 12 18
>>
>>
>> Could you tell me how to do it in R ? Thank you very much!
>>
>>
>> Best regards,
>> Chunyu
>>
>>
>>
>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at 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 at 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.