zhengxwen / gdsfmt

R Interface to CoreArray Genomic Data Structure (GDS) Files (Development version only)
http://www.bioconductor.org/packages/gdsfmt
18 stars 4 forks source link

Transpose an array by permuting its dimensions #5

Closed zhengxwen closed 9 years ago

zhengxwen commented 9 years ago

Here is an example using apply.gdsn with a target GDS node:

library(gdsfmt)

# cteate a GDS file
f <- createfn.gds("test.gds")

(n1 <- add.gdsn(f, "array", val=array(1:120, dim=c(5,4,3,2))))
read.gdsn(n1)

showing:

, , 1, 1
     [,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

, , 2, 1
     [,1] [,2] [,3] [,4]
[1,]   21   26   31   36
[2,]   22   27   32   37
[3,]   23   28   33   38
[4,]   24   29   34   39
[5,]   25   30   35   40

, , 3, 1
     [,1] [,2] [,3] [,4]
[1,]   41   46   51   56
[2,]   42   47   52   57
[3,]   43   48   53   58
[4,]   44   49   54   59
[5,]   45   50   55   60

, , 1, 2
     [,1] [,2] [,3] [,4]
[1,]   61   66   71   76
[2,]   62   67   72   77
[3,]   63   68   73   78
[4,]   64   69   74   79
[5,]   65   70   75   80

, , 2, 2
     [,1] [,2] [,3] [,4]
[1,]   81   86   91   96
[2,]   82   87   92   97
[3,]   83   88   93   98
[4,]   84   89   94   99
[5,]   85   90   95  100

, , 3, 2
     [,1] [,2] [,3] [,4]
[1,]  101  106  111  116
[2,]  102  107  112  117
[3,]  103  108  113  118
[4,]  104  109  114  119
[5,]  105  110  115  120

Then use aperm with a parameter perm=c(2,1,3) to permute the dimensions:

n1.1 <- add.gdsn(f, "permuting", storage="int", valdim=c(4,5,3,0))
apply.gdsn(n1, margin=4, FUN=aperm, as.is="gdsnode", target.node=n1.1,
    perm=c(2,1,3))

read.gdsn(n1.1)
closefn.gds(f)

showing:

, , 1, 1
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    6    7    8    9   10
[3,]   11   12   13   14   15
[4,]   16   17   18   19   20

, , 2, 1
     [,1] [,2] [,3] [,4] [,5]
[1,]   21   22   23   24   25
[2,]   26   27   28   29   30
[3,]   31   32   33   34   35
[4,]   36   37   38   39   40

, , 3, 1
     [,1] [,2] [,3] [,4] [,5]
[1,]   41   42   43   44   45
[2,]   46   47   48   49   50
[3,]   51   52   53   54   55
[4,]   56   57   58   59   60

, , 1, 2
     [,1] [,2] [,3] [,4] [,5]
[1,]   61   62   63   64   65
[2,]   66   67   68   69   70
[3,]   71   72   73   74   75
[4,]   76   77   78   79   80

, , 2, 2
     [,1] [,2] [,3] [,4] [,5]
[1,]   81   82   83   84   85
[2,]   86   87   88   89   90
[3,]   91   92   93   94   95
[4,]   96   97   98   99  100

, , 3, 2
     [,1] [,2] [,3] [,4] [,5]
[1,]  101  102  103  104  105
[2,]  106  107  108  109  110
[3,]  111  112  113  114  115
[4,]  116  117  118  119  120