[R] About 5.1 Arrays

Steve Lianoglou mailinglist.honeypot at gmail.com
Fri Nov 5 15:18:18 CET 2010


Hi,

On Fri, Nov 5, 2010 at 6:00 AM, Stephen Liu <satimis at yahoo.com> wrote:
[snip]
>> A[i, j, k] is the value of the element in position (i,j,k) of array A. In
>> other words, it is the entry in row i, column j, and "layer" k (if one
>> wants to think of A as a cuboidal grid).
>
> Sorry I can't follow.  Could you pls explain in more detail.
>
> e.g.
>
>> z <- 0:23
>> dim(z) <- c(3,4,2)
>> dim(z)
> [1] 3 4 2
>
>
>> z
> , , 1
>
>     [,1] [,2] [,3] [,4]
> [1,]    0    3    6    9
> [2,]    1    4    7   10
> [3,]    2    5    8   11
>
> , , 2
>
>     [,1] [,2] [,3] [,4]
> [1,]   12   15   18   21
> [2,]   13   16   19   22
> [3,]   14   17   20   23

It's not clear what you're having problems understanding. By setting
the "dim" attribute of your (1d) vector, you are changing its
dimenensions.

## This is a 1d vector
R> x <- 1:12
R> x

## I can change it into 2d (like a matrix), let's do 2 rows, 6 columns
R> dim(x) <- c(2,6)
R>      [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    3    5    7    9   11
[2,]    2    4    6    8   10   12

If you understand that using three numbers to set the dimension means
you are making a 3d matrix

R> dim(x) <- c(2,3,2)

But the problem is you can't draw 3d in a terminal, so it just draws
the third dimension in order

R> x

x
, , 1

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

, , 2

     [,1] [,2] [,3]
[1,]    7    9   11
[2,]    8   10   12

###

Imagine this as a cube: ,,1 is the front layer, ,,2 is the back layer.

Just chew on it for a minute, it'll make sense.

-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact



More information about the R-help mailing list