On Thu, May 03, 2012 at 07:36:45PM -0700, marc212 wrote:
> I have the following:
> Time   A1   A1   B1   B1   C1   C2
>               x     y     x      y       x     y
>  0         5     6      6      7      7      9
>   1         3     4      4      3      9      9  
>   2         5     2      6      4     7       4
> 
> I want to change it to the following:
>         0           1             2
>         x    y     x    y     x    y 
> A1  5    6     3   4      5    2
> B1  6    7     4   3      6   4
> etc for a much larger set

Hi.

Try the following.

  # the example input
  Orig <- rbind(
  "0"=c(5, 6, 6, 7, 7, 9),
  "1"=c(3, 4, 4, 3, 9, 9),
  "2"=c(5, 2, 6, 4, 7, 4))
  colnames(Orig) <- rep(c("x", "y"), times=3)

  # transformation
  Arr <- array(Orig, dim=c(nrow(Orig), 2, ncol(Orig)/2))
  Arr

  , , 1    # this is the required row 1 in the output
  
       [,1] [,2]
  [1,]    5    6
  [2,]    3    4
  [3,]    5    2
  
  , , 2    # this is the required row 2 in the output
  
       [,1] [,2]
  [1,]    6    7
  [2,]    4    3
  [3,]    6    4
  
  , , 3
  
       [,1] [,2]
  [1,]    7    9
  [2,]    9    9
  [3,]    7    4

  Arr1 <- aperm(Arr, perm=c(2, 1, 3))
  d <- dim(Arr1)
  t(array(Arr1, dim=c(d[1]*d[2], d[3])))

       [,1] [,2] [,3] [,4] [,5] [,6]
  [1,]    5    6    3    4    5    2
  [2,]    6    7    4    3    6    4
  [3,]    7    9    9    9    7    4

Hope this helps.

Petr Savicky.

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

Reply via email to