If your columns are already sorted in the right way, just make the matrix
into an array and aperm() as needed. Here's an example:
> m
A1.B1.C1 A2.B1.C1 A1.B2.C1 A2.B2.C1 A1.B1.C2 A2.B1.C2 A1.B2.C2 A2.B2.C2
1 1 3 5 7 9 11 13 15
2 2 4 6 8 10 12 14 16
> ma <- array(m, dim=c(2, 2, 2, 2))
> ma
, , 1, 1
[,1] [,2]
[1,] 1 3
[2,] 2 4
, , 2, 1
[,1] [,2]
[1,] 5 7
[2,] 6 8
, , 1, 2
[,1] [,2]
[1,] 9 11
[2,] 10 12
, , 2, 2
[,1] [,2]
[1,] 13 15
[2,] 14 16
> aperm(ma, c(1,4:2))
, , 1, 1
[,1] [,2]
[1,] 1 9
[2,] 2 10
, , 2, 1
[,1] [,2]
[1,] 5 13
[2,] 6 14
, , 1, 2
[,1] [,2]
[1,] 3 11
[2,] 4 12
, , 2, 2
[,1] [,2]
[1,] 7 15
[2,] 8 16
Andy
From: Xao Ping
Dear All:
I would appreciate an advice on the following matter. I have a data set
organized as a matrix with 20000 rows and 72 columns. The column names
represent a chierarchical structure. Namely, there is 6 logically different
groups, each containing 12 columns. There are 4 sub-groups within each of
these 6 groups, each containing 3 columns. I would like to transform the 2D
data matrix into 4D array with dim=c(20000,6,4,3). Is there any smart way
to perform this task without resorting to "for" loops?
Thank you in advance
Xao Ping
R&R Pharmakinetics
Taiwan
Do you Yahoo!?
Yahoo! Mail - Find what you need with new enhanced search. Learn more.
------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachments, contains
information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station, New
Jersey, USA 08889), and/or its affiliates (which may be known outside the
United States as Merck Frosst, Merck Sharp & Dohme or MSD and in Japan, as
Banyu) that may be confidential, proprietary copyrighted and/or legally
privileged. It is intended solely for the use of the individual or entity named
on this message. If you are not the intended recipient, and have received this
message in error, please notify us immediately by reply e-mail and then delete
it from your system.
------------------------------------------------------------------------------
|