[R] About 5.1 Arrays

Stephen Liu satimis at yahoo.com
Sun Nov 7 03:31:18 CET 2010


Hi Daniel,


> I am correcting you. :-)  You are using dim() incorrectly, and not  accessing 
>the array correctly.  
>
> In all of your examples you should be  using dim(3,4,2).  Then you need to 
>specify the indexes 
>
> of the array  element you want to look at. So, to use your example

Thanks for your correction.  So the index to be used in my example should be 
(3,4,2) only.


But still I'm not very clear re your advice on follows

> a<-1:24
> a3d <- array(a, dim = c(3,4,2))
> a3d
, , 1

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

, , 2

     [,1] [,2] [,3] [,4]
[1,]   13   16   19   22
[2,]   14   17   20   23
[3,]   15   18   21   24

> 
> # 1 maps to a[1, 1, 1] in the 3d array
> a3d[1, 1, 1]
[1] 1
> 
> # 2 maps to a[2, 1, 1].
> a3d[2, 1, 1]
[1] 2
> 
> # 3 maps to a[3, 1, 1]
> a3d[3, 1, 1]
[1] 3
> 
> # 4 maps to a[1, 2, 1]
> a3d[1, 2, 1]
[1] 4


What does it mean;

[1] 1
[1] 2
[1] 3
[1] 4

as mentioned ?

Anyway I'll move/continue on the manual to see what will happen.

B.R.
Stephen L






----- Original Message ----
From: Daniel Nordlund <djnordlund at frontier.com>
To: r-help at r-project.org
Sent: Sun, November 7, 2010 2:08:04 AM
Subject: Re: [R] About 5.1 Arrays

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of Stephen Liu
> Sent: Saturday, November 06, 2010 7:38 AM
> To: Joshua Wiley
> Cc: r-help at r-project.org
> Subject: Re: [R] About 5.1 Arrays
> 
> Hi Joshua,
> 
> Thanks for your advice.
> 
> 1)
> Re your advice:-[quote]
> > a3d
> , , 1 <--- this is the first position of the third dimension
> 
>      [,1] [,2] [,3] [,4]  <--- positions 1, 2, 3, 4 of the second
> dimension
> [1,]    1    4    7   10
> [2,]    2    5    8   11
> [3,]    3    6    9   12
> ^  the first dimension
> 
> , , 2 <--- the second position of the third dimension
> ...
> [/quote]
> 
> Where is the third dimension?
> 
> 
> 2)
> Re your advice:-[quote]
> so you can think that in the original vector "a":
> 1 maps to a[1, 1, 1] in the 3d array
> 2 maps to a[2, 1, 1].
> 3 maps to a[3, 1, 1]
> 4 maps to a[1, 2, 1]
> 12 maps to a[3, 4, 1]
> 20 maps to a[2, 3, 2]
> 24 maps to a[3, 4, 2]
> [/quote]
> 
> My finding;
> 
> # 1 maps to a[1, 1, 1] in the 3d array
> > a3d <- array(a, dim = c(1, 1, 1))
> > a3d
> , , 1
> 
>      [,1]
> [1,]    1
> 
> Correct
> 
> # 2 maps to a[2, 1, 1].
> > a3d <- array(a, dim = c(2, 1, 1))
> > a3d
> , , 1
> 
>      [,1]
> [1,]    1
> [2,]    2
> 
> Correct
> 
> # 3 maps to a[3, 1, 1]
> > a3d <- array(a, dim = c(3, 1, 1))
> > a3d
> , , 1
> 
>      [,1]
> [1,]    1
> [2,]    2
> [3,]    3
> 
> Correct
> 
> # 4 maps to a[1, 2, 1]
> > a3d <- array(a, dim = c(1, 2, 1))
> > a3d
> , , 1
> 
>      [,1] [,2]
> [1,]    1    2
> 
> Incorrect.  It is "2"
> 
> 
> # 12 maps to a[3, 4, 1]
> > a3d <- array(a, dim = c(3, 4, 1))
> > a3d
> , , 1
> 
>      [,1] [,2] [,3] [,4]
> [1,]    1    4    7   10
> [2,]    2    5    8   11
> [3,]    3    6    9   12
> 
> Correct
> 
> # 20 maps to a[2, 3, 2]
> > a3d <- array(a, dim = c(2, 3, 2))
> > a3d
> , , 1
> 
>      [,1] [,2] [,3]
> [1,]    1    3    5
> [2,]    2    4    6
> 
> , , 2
> 
>      [,1] [,2] [,3]
> [1,]    7    9   11
> [2,]    8   10   12
> 
> Incorrect.  It is "12"
> 
> 
> #  24 maps to a[3, 4, 2]
> > a3d <- array(a, dim = c(3, 4, 2))
> > a3d
> , , 1
> 
>      [,1] [,2] [,3] [,4]
> [1,]    1    4    7   10
> [2,]    2    5    8   11
> [3,]    3    6    9   12
> 
> , , 2
> 
>      [,1] [,2] [,3] [,4]
> [1,]   13   16   19   22
> [2,]   14   17   20   23
> [3,]   15   18   21   24
> 
> Correct.
> 
> If I'm wrong, pls correct me.  Thanks
> 
> 
> B.R.
> Stephen
> 

Stephen,

I am correcting you. :-)  You are using dim() incorrectly, and not accessing the 
array correctly.  In all of your examples you should be using dim(3,4,2).  Then 
you need to specify the indexes of the array element you want to look at. So, to 
use your example

> a<-1:24
> a3d <- array(a, dim = c(3,4,2))
> a3d
, , 1

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

, , 2

     [,1] [,2] [,3] [,4]
[1,]   13   16   19   22
[2,]   14   17   20   23
[3,]   15   18   21   24

> 
> # 1 maps to a[1, 1, 1] in the 3d array
> a3d[1, 1, 1]
[1] 1
> 
> # 2 maps to a[2, 1, 1].
> a3d[2, 1, 1]
[1] 2
> 
> # 3 maps to a[3, 1, 1]
> a3d[3, 1, 1]
[1] 3
> 
> # 4 maps to a[1, 2, 1]
> a3d[1, 2, 1]
[1] 4


Hope this is helpful,

Dan

Daniel Nordlund
Bothell, WA USA


______________________________________________
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