[R] converting matrix in array

William Dunlap wdunlap at tibco.com
Sun Aug 28 23:42:14 CEST 2011


It helps to consider a small self-containd example
where the correct answer is obvious.  Does the following
look like your input and desired output?

  > a <- rbind(c("obj1-ptA-x", "obj1-ptA-y", "obj1-ptA-z"),
  +            c("obj1-ptB-x", "obj1-ptB-y", "obj1-ptB-z"),
  +            c("obj2-ptA-x", "obj2-ptA-y", "obj2-ptA-z"),
  +            c("obj2-ptB-x", "obj2-ptB-y", "obj2-ptB-z"))
  > a
               [,1]         [,2]         [,3]
  [1,] "obj1-ptA-x" "obj1-ptA-y" "obj1-ptA-z"
  [2,] "obj1-ptB-x" "obj1-ptB-y" "obj1-ptB-z"
  [3,] "obj2-ptA-x" "obj2-ptA-y" "obj2-ptA-z"
  [4,] "obj2-ptB-x" "obj2-ptB-y" "obj2-ptB-z"
  > aperm(array(a, c(2, 2, 3)), c(1,3,2))
  , , 1
  
               [,1]         [,2]         [,3]
  [1,] "obj1-ptA-x" "obj1-ptA-y" "obj1-ptA-z"
  [2,] "obj1-ptB-x" "obj1-ptB-y" "obj1-ptB-z"
  
  , , 2
  
               [,1]         [,2]         [,3]
  [1,] "obj2-ptA-x" "obj2-ptA-y" "obj2-ptA-z"
  [2,] "obj2-ptB-x" "obj2-ptB-y" "obj2-ptB-z"

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of marco
> Sent: Sunday, August 28, 2011 2:10 PM
> To: r-help at r-project.org
> Subject: [R] converting matrix in array
> 
> Hi everyone,
> have a small problem trying to converting a dataset in matrix form to an
> array.
> Specifically: data include 3D measurement -x,y,z of 59 points in 36 objects.
> They are stored as a matrix (x) of 2124 rows and 3 columns.
> What I want to do is to extract each subject's dataset using an array (b).
> Accordingly, I tried the following command:
> 
> b<-array(a,c(59,3,36)).
> 
> The problem is that the resulting array for some strange reason change the
> order of the original data.
> Specifically, I noticed that, for example, in the first subject, the x y z
> values of the first point are the x values of the first 3 subjects.
> 
> Did I perhaps missed something?
> Thanks in advance
> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/converting-matrix-in-array-
> tp3775025p3775025.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> 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