[R] About 5.1 Arrays
Joshua Wiley
jwiley.psych at gmail.com
Fri Nov 5 17:48:27 CET 2010
On Fri, Nov 5, 2010 at 9:17 AM, Stephen Liu <satimis at yahoo.com> wrote:
> Hi Daniel,
>
> Thanks for your detail advice. I completely understand your explain.
>
> But I can't resolve what does "a" stand for there?
the "a" just represents some vector. It is the name of the object
that stores your data. Like you might tell someone to go look in a
book to find some information.
>
> a[1,1,1] is 1 * 1 * 1 = 1
> a[2,1,1] is 2 * 1 * 1 = 2
> a[2,4,2] is 2 * 4 * 2 = 16
> a[3,4,2] is 3 * 4 * 2 = 24
That is the basic idea, but it may not be the most helpful way to
think of it because it depends on the length of the each dimension.
For example
a[1, 2, 1] is not 1 * 2 * 1 = 2
a[1, 1, 2] is not 1 * 1 * 2 = 2
in the little 3d array I show below, it would actually be
a[1, 2, 1] = 4
a[1, 1, 2] = 13
>
> ?
>
>
> B.R.
> Stephen L
>
> ----- Original Message ----
> From: Daniel Nordlund <djnordlund at frontier.com>
> To: r-help at r-project.org
> Sent: Fri, November 5, 2010 11:54:15 PM
> 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: Friday, November 05, 2010 7:57 AM
>> To: Steve Lianoglou
>> Cc: r-help at r-project.org
>> Subject: Re: [R] About 5.1 Arrays
>>
>> Hi Steve,
>>
>> > It's not clear what you're having problems understanding. By
>> > setting the "dim" attribute of your (1d) vector, you are changing
>> > itsdimenensions.
>>
>> I'm following An Introduction to R to learn R
>>
>> On
>>
>> 5.1 Arrays
>> http://cran.r-project.org/doc/manuals/R-intro.html#Vectors-and-assignment
>>
>>
>> It mentions:-
>> ...
>> For example if the dimension vector for an array, say a, is c(3,4,2) then
>> there
>> are 3 * 4 * 2 = 24 entries in a and the data vector holds them in the
>> order
>> a[1,1,1], a[2,1,1], ..., a[2,4,2], a[3,4,2].
>>
>>
>> I don't understand "on .... =24 entries in a and the data vector holds
>> them in
>> the order a[1,1,1], a[2,1,1], ..., a[2,4,2], a[3,4,2]." the order
>> a[1,1,1],
>> a[2,1,1], ..., a[2,4,2], a[3,4,2]? What does it mean "the order a[1,1,1],
>> a[2,1,1], ..., a[2,4,2], a[3,4,2]"?
because it is actually stored as a 1 dimensional vector, it is just
telling you the order. For example, given some vector "a" that
contains the numbers 1 through 24, you could reshape this into a three
dimensional object. It would be stored like:
# make a vector "a" and an array (built from "a") called a3d
> a <- 1:24
> a3d <- array(a, dim = c(3, 4, 2))
> a
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
> 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
[,1] [,2] [,3] [,4]
[1,] 13 16 19 22
[2,] 14 17 20 23
[3,] 15 18 21 24
a[1, 1, 1] is the first element of dimension 1, the first element of
dimension 2, and the first element of dimension 3. so 1.
a[2, 1, 1] is the *second* element of dimension 1, the first element
of dimension 2, and the first element of dimension 3. so 2
a[3, 4, 2] is the third element of dimension 1, the fourth element of
dimension 2, and the second element of dimension 3. so 24.
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]
>>
>> Thanks
>>
>> B.R.
>> Stephen
<snip>
--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/
More information about the R-help
mailing list