Re: [R] About 5.1 Arrays
Hi Joshua, .You can think of a 3d array kind of like a journal (I don't know if this is helpful, but I kind of like the analogy so...). Each page holds a two dimensional table, so I could tell you to look at row 4, column 3 on page 16. Nevertheless, at any given point, it is just a flat page. A good example. It can be regarded as a book with multiple pages. Each page holds a table of 2 dimensions. No, it is correct. You cannot assume that you may use the same indices to access an array when you have created it with different dimensions. Consider: array(1:24, dim = c(3, 4, 2))[1, 2, 1] [1] 4 array(1:24, dim = c(1, 2, 1))[1, 2, 1] [1] 2 I understand now, e.g. array(1:24, dim = c(3, 4, 2))[2,3,2] [1] 20 Not array(1:24, dim = c(2, 3, 2))[2,3,2] [1] 12 Lot of thanks for your advice and effort. B.R. Stephen L - Original Message From: Joshua Wiley jwiley.ps...@gmail.com To: Stephen Liu sati...@yahoo.com Cc: r-help@r-project.org Sent: Mon, November 8, 2010 8:33:50 AM Subject: Re: [R] About 5.1 Arrays On Sat, Nov 6, 2010 at 7:38 AM, Stephen Liu sati...@yahoo.com wrote: Hi Joshua, Thanks for your advice. 1) Re your advice:-[quote] a3d , , 1 --- this is the first position of the third dimension ***THIS IS THE THIRD DIMENSION*** [,1] [,2] [,3] [,4] --- positions 1, 2, 3, 4 of the second dimension [1,]147 10 [2,]258 11 [3,]369 12 ^ the first dimension , , 2 --- the second position of the third dimension ***THIS IS THE THIRD DIMENSION*** ... [/quote] Where is the third dimension? I pointed to the third dimension above. You can think of a 3d array kind of like a journal (I don't know if this is helpful, but I kind of like the analogy so...). Each page holds a two dimensional table, so I could tell you to look at row 4, column 3 on page 16. Nevertheless, at any given point, it is just a flat page. 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,]12 Incorrect. It is 2 No, it is correct. You cannot assume that you may use the same indices to access an array when you have created it with different dimensions. Consider: array(1:24, dim = c(3, 4, 2))[1, 2, 1] [1] 4 array(1:24, dim = c(1, 2, 1))[1, 2, 1] [1] 2 # 12 maps to a[3, 4, 1] a3d - array(a, dim = c(3, 4, 1)) a3d , , 1 [,1] [,2] [,3] [,4] [1,]147 10 [2,]258 11 [3,]369 12 Correct # 20 maps to a[2, 3, 2] a3d - array(a, dim = c(2, 3, 2)) a3d , , 1 [,1] [,2] [,3] [1,]135 [2,]246 , , 2 [,1] [,2] [,3] [1,]79 11 [2,]8 10 12 Incorrect. It is 12 See my above comment about not expecting things in the same location when you change the space they live in. Sorry this was so slow in coming, I missed the email somehow. Cheers, Josh [snip] __ 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.
Re: [R] About 5.1 Arrays
Hi Stephan Since early in November you have posted queries on R-help on 5 different topics as you are trying to learn to use R. This is a good thing, and many contributors to R-help have replied, taking pains to provide explanations and examples to be helpful. But, in return, it doesn't appear that you've done very much work on your own to help your understanding of arrays in R, since you keep posting follow-ups on aspects that had been explained before. This list is r-help, not r-tutor or r-you-do-my-thinking-for-me. On 11/5/2010 4:22 AM, Stephen Liu wrote: Hi folks, (Learning R) 5.1 Arrays http://cran.r-project.org/doc/manuals/R-intro.html#Vectors-and-assignment 1) If continued on previous example (3.1 Intrinsic attributes: mode and length), z- 0:9 dim(z)- c(3,5,100) Error in dim(z)- c(3, 5, 100) : dims [product 1500] do not match the length of object [10] failed. Error messages in R are brief, and sometimes obscure, but what did you not understand from that error message? Cancel that. I don't want to know. 2) Ran; z- 0:1499 dim(z)- c(3,5,100) dim(z) [1] 3 5 100 It worked OR 3) z- 1:1500 dim(z)- c(3,5,100) dim(z) [1] 3 5 100 It also worked. z [1]123456789 10 11 12 13 14 [15] 15 16 17 18 19 20 21 22 23 24 25 26 27 28 . [1485] 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 [1499] 1499 1500 0 is counted as 1 object. Does object length mean the total number of objects/entries? Please help me to understand follow; 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; a[1,1,1], a[2,1,1], ..., a[2,4,2] 1 * 1 * 1 / 2 * 1 * 1 / 2 * 4 * 2 is NOT 24 ? TIA B.R. Stephen L __ 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.
Re: [R] About 5.1 Arrays
On Sat, Nov 6, 2010 at 7:38 AM, Stephen Liu sati...@yahoo.com wrote: Hi Joshua, Thanks for your advice. 1) Re your advice:-[quote] a3d , , 1 --- this is the first position of the third dimension ***THIS IS 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 ***THIS IS THE THIRD DIMENSION*** ... [/quote] Where is the third dimension? I pointed to the third dimension above. You can think of a 3d array kind of like a journal (I don't know if this is helpful, but I kind of like the analogy so...). Each page holds a two dimensional table, so I could tell you to look at row 4, column 3 on page 16. Nevertheless, at any given point, it is just a flat page. 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 No, it is correct. You cannot assume that you may use the same indices to access an array when you have created it with different dimensions. Consider: array(1:24, dim = c(3, 4, 2))[1, 2, 1] [1] 4 array(1:24, dim = c(1, 2, 1))[1, 2, 1] [1] 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 See my above comment about not expecting things in the same location when you change the space they live in. Sorry this was so slow in coming, I missed the email somehow. Cheers, Josh [snip] __ 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.
Re: [R] About 5.1 Arrays
Hi Richard, ## for an array with ## dim(a) == c(3,4,2) ## a[i,j,k] means select the element in position ##i + (j-1)*3 + (k-1)*3*4 My understanding; e.g. 1) dim(a) == c(3,4,2) 3 + (4-1)*3 + (2-1)*3*4 3+9+12=24 2) ## dim(a) == c(1,2,1) 1 + (2-1)*3 + (1-1)*3*4 1+3+0=4 3) ## dim(a) == c(2,3,1) 2 + (3-1)*3 + (1-1)*3*4 2+6+0=8 etc. It is NOT always the product of i*j*k as I thought before. Thanks for your explanation. What are the value of 3 and 4? The values of position and dimension? e.g. a - sample(24) a [1] 22 18 17 10 24 1 11 13 9 19 20 8 2 21 23 16 7 14 12 15 4 5 3 6 dim(a) - c(3,4,2) a , , 1 [,1] [,2] [,3] [,4] -- positions 1, 2, 3, 4 of the second dimension ? [1,] 22 10 11 19 [2,] 18 24 13 20 [3,] 17198 ^ the first dimension ? , , 2 [,1] [,2] [,3] [,4] [1,]2 16 125 [2,] 217 153 [3,] 23 1446 ? If I'm wrong pls correct me. TIA Now I'm going to digest Joshua's advice. B.R. Stephen L From: RICHARD M. HEIBERGER r...@temple.edu Cc: Daniel Nordlund djnordl...@frontier.com; r-help@r-project.org Sent: Sat, November 6, 2010 12:48:35 AM Subject: Re: [R] About 5.1 Arrays Continuing with Daniel's example, but with different data values a - sample(24) a dim(a) - c(3,4,2) a as.vector(a) ## for an array with ## dim(a) == c(3,4,2) ## a[i,j,k] means select the element in position ##i + (j-1)*3 + (k-1)*3*4 index - function(i,j,k) { i + (j-1)*3 + (k-1)*3*4 } ## find the vector position described by row 2, column 1, layer 2 index(2,1,2)## this is the position in the original vector a[2,1,2]## this is the value in that position with 3D indexing a[index(2,1,2)] ## this is the same value with 1D vector indexing a[14] ## this is the same value with 1D vector indexing ## find the position in row 3, column 4, layer 1 index(3,4,1)## this is the position in the original vector a[3,4,1]## this is the value in that position with 3D indexing a[index(3,4,1)] ## this is the same value with 1D vector indexing a[12] ## this is the same value with 1D vector indexing index(1,1,1)## this is the position in the original vector index(2,1,1)## this is the position in the original vector index(3,1,1)## this is the position in the original vector index(1,2,1)## this is the position in the original vector index(2,2,1)## this is the position in the original vector index(3,2,1)## this is the position in the original vector index(1,3,1)## this is the position in the original vector index(2,3,1)## this is the position in the original vector index(3,3,1)## this is the position in the original vector index(1,4,1)## this is the position in the original vector index(2,4,1)## this is the position in the original vector index(3,4,1)## this is the position in the original vector index(1,1,2)## this is the position in the original vector index(2,1,2)## this is the position in the original vector index(3,1,2)## this is the position in the original vector index(1,2,2)## this is the position in the original vector index(2,2,2)## this is the position in the original vector index(3,2,2)## this is the position in the original vector index(1,3,2)## this is the position in the original vector index(2,3,2)## this is the position in the original vector index(3,3,2)## this is the position in the original vector index(1,4,2)## this is the position in the original vector index(2,4,2)## this is the position in the original vector index(3,4,2)## this is the position in the original vector [[alternative HTML version deleted]] __ 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.
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,]147 10 [2,]258 11 [3,]369 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,]12 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,]147 10 [2,]258 11 [3,]369 12 Correct # 20 maps to a[2, 3, 2] a3d - array(a, dim = c(2, 3, 2)) a3d , , 1 [,1] [,2] [,3] [1,]135 [2,]246 , , 2 [,1] [,2] [,3] [1,]79 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,]147 10 [2,]258 11 [3,]369 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 - Original Message From: Joshua Wiley jwiley.ps...@gmail.com To: Stephen Liu sati...@yahoo.com Cc: r-help@r-project.org Sent: Sat, November 6, 2010 12:48:27 AM Subject: Re: [R] About 5.1 Arrays On Fri, Nov 5, 2010 at 9:17 AM, Stephen Liu sati...@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 djnordl...@frontier.com To: r-help@r-project.org Sent: Fri, November 5, 2010 11:54:15 PM Subject: Re: [R] About 5.1 Arrays -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Stephen Liu Sent: Friday, November 05, 2010 7:57 AM To: Steve Lianoglou Cc: r-help@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,]147 10 [2,]258 11 [3,]369 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
Re: [R] About 5.1 Arrays
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Stephen Liu Sent: Saturday, November 06, 2010 7:38 AM To: Joshua Wiley Cc: r-help@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,]147 10 [2,]258 11 [3,]369 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,]12 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,]147 10 [2,]258 11 [3,]369 12 Correct # 20 maps to a[2, 3, 2] a3d - array(a, dim = c(2, 3, 2)) a3d , , 1 [,1] [,2] [,3] [1,]135 [2,]246 , , 2 [,1] [,2] [,3] [1,]79 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,]147 10 [2,]258 11 [3,]369 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,]147 10 [2,]258 11 [3,]369 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@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.
Re: [R] About 5.1 Arrays
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,]147 10 [2,]258 11 [3,]369 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 djnordl...@frontier.com To: r-help@r-project.org Sent: Sun, November 7, 2010 2:08:04 AM Subject: Re: [R] About 5.1 Arrays -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Stephen Liu Sent: Saturday, November 06, 2010 7:38 AM To: Joshua Wiley Cc: r-help@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,]147 10 [2,]258 11 [3,]369 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,]12 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,]147 10 [2,]258 11 [3,]369 12 Correct # 20 maps to a[2, 3, 2] a3d - array(a, dim = c(2, 3, 2)) a3d , , 1 [,1] [,2] [,3] [1,]135 [2,]246 , , 2 [,1] [,2] [,3] [1,]79 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,]147 10 [2,]258 11 [3,]369 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,]147 10 [2,]258 11 [3,]369 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@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. __ 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.
Re: [R] About 5.1 Arrays
On Fri, 5 Nov 2010, Stephen Liu wrote: [snip] 0 is counted as 1 object. Of course! It is a number like any other. Does object length mean the total number of objects/entries? Yes. Please help me to understand follow; 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; a[1,1,1], a[2,1,1], ..., a[2,4,2] [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). Hth -- Gerrit - AOR Dr. Gerrit Eichner Mathematical Institute, Room 212 gerrit.eich...@math.uni-giessen.de Justus-Liebig-University Giessen Tel: +49-(0)641-99-32104 Arndtstr. 2, 35392 Giessen, Germany Fax: +49-(0)641-99-32109http://www.uni-giessen.de/cms/eichner __ 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.
Re: [R] About 5.1 Arrays
Hi Gerrit, Thanks for your advice. - 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,]0369 [2,]147 10 [3,]258 11 , , 2 [,1] [,2] [,3] [,4] [1,] 12 15 18 21 [2,] 13 16 19 22 [3,] 14 17 20 23 TIA B.R. Stephen L - AOR Dr. Gerrit Eichner Mathematical Institute, Room 212 gerrit.eich...@math.uni-giessen.de Justus-Liebig-University Giessen Tel: +49-(0)641-99-32104 Arndtstr. 2, 35392 Giessen, Germany Fax: +49-(0)641-99-32109http://www.uni-giessen.de/cms/eichner - __ 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.
Re: [R] About 5.1 Arrays
Hi, On Fri, Nov 5, 2010 at 6:00 AM, Stephen Liu sati...@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,]13579 11 [2,]2468 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,]135 [2,]246 , , 2 [,1] [,2] [,3] [1,]79 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 __ 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.
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]? Thanks B.R. Stephen - Original Message From: Steve Lianoglou mailinglist.honey...@gmail.com To: Stephen Liu sati...@yahoo.com Cc: Gerrit Eichner gerrit.eich...@math.uni-giessen.de; r-help@r-project.org Sent: Fri, November 5, 2010 10:18:18 PM Subject: Re: [R] About 5.1 Arrays Hi, On Fri, Nov 5, 2010 at 6:00 AM, Stephen Liu sati...@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,]0369 [2,]147 10 [3,]258 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,]13579 11 [2,]2468 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,]135 [2,]246 , , 2 [,1] [,2] [,3] [1,]79 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 __ 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.
Re: [R] About 5.1 Arrays
Hi, On Fri, Nov 5, 2010 at 10:56 AM, Stephen Liu sati...@yahoo.com wrote: 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]? Let's just stick with a 2d matrix -- it's easier to think about. I'm not sure if you are coming from a different programming language or not, so perhaps this isn't helpful if you don't, but you might imagine holding data for a 2d matrix in an 'array of arrays' structure. R doesn't do this. It holds the data for a 1d, 2d, 3d, ... 10d array in a 1d vector. The data is stored in column major format, so the rows of a 2d matrix are filled first. If I have a 2d matrix like this: 1 2 3 4 5 6 7 8 R holds this in a 1d vector/array that looks like this: 1, 5, 2, 6, 3, 7, 4, 8 This idea follows through to higher dimensions. Hope that helps, -steve -- 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 __ 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.
Re: [R] About 5.1 Arrays
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Stephen Liu Sent: Friday, November 05, 2010 7:57 AM To: Steve Lianoglou Cc: r-help@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]? Thanks B.R. Stephen Stephen, Start with a vector of length = 12. The vector, v, is stored in consecutive locations in memory, one after the other. And v - 1:12 v [1] 1 2 3 4 5 6 7 8 9 10 11 12 Now change then change the dimension of v to c(3,4), i.e. a matrix with 3 rows and 4 columns. dim(v) - c(3,4) v [,1] [,2] [,3] [,4] [1,]147 10 [2,]258 11 [3,]369 12 The values of v are still stored in memory in consecutive locations. But now you refer to the first location as v[1,1], the second as v[2,1], third as v[3,1] ... and the 12th as v[3,4]. We sometimes talk about the values going into v[1,1] or more generally, v[i,j], but the values aren't going anywhere. They are still stored in consecutive locations. We are just changing how they are referred to when we change the dimensions. So in the 2-dimensional matrix above, the values of the vector v go into the matrix in column order, i.e. the first column is filled first, then the second, ... Now, create a 24 element vector. v - 1:24 v [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 Change the dimensions to a 3-dimensional array. dim(v) - c(3,4,2) v , , 1 [,1] [,2] [,3] [,4] [1,]147 10 [2,]258 11 [3,]369 12 , , 2 [,1] [,2] [,3] [,4] [1,] 13 16 19 22 [2,] 14 17 20 23 [3,] 15 18 21 24 You can visualize a 3-dimensional array as a series of 2-dimensional arrays stacked on top of each other. But this is just a convenient image. The items are still stored consecutively in memory. Notice that layer one in the stack was filled first, and the first layer was filled just like the previous 2-dimensional example. But the items are still physically stored linearly, in consecutive locations in memory. Hope this is helpful, Dan Daniel Nordlund Bothell, WA USA __ 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.
Re: [R] About 5.1 Arrays
Hi Daniel, Thanks for your detail advice. I completely understand your explain. But I can't resolve what does a stand for there? 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 ? B.R. Stephen L - Original Message From: Daniel Nordlund djnordl...@frontier.com To: r-help@r-project.org Sent: Fri, November 5, 2010 11:54:15 PM Subject: Re: [R] About 5.1 Arrays -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Stephen Liu Sent: Friday, November 05, 2010 7:57 AM To: Steve Lianoglou Cc: r-help@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]? Thanks B.R. Stephen Stephen, Start with a vector of length = 12. The vector, v, is stored in consecutive locations in memory, one after the other. And v - 1:12 v [1] 1 2 3 4 5 6 7 8 9 10 11 12 Now change then change the dimension of v to c(3,4), i.e. a matrix with 3 rows and 4 columns. dim(v) - c(3,4) v [,1] [,2] [,3] [,4] [1,]147 10 [2,]258 11 [3,]369 12 The values of v are still stored in memory in consecutive locations. But now you refer to the first location as v[1,1], the second as v[2,1], third as v[3,1] ... and the 12th as v[3,4]. We sometimes talk about the values going into v[1,1] or more generally, v[i,j], but the values aren't going anywhere. They are still stored in consecutive locations. We are just changing how they are referred to when we change the dimensions. So in the 2-dimensional matrix above, the values of the vector v go into the matrix in column order, i.e. the first column is filled first, then the second, ... Now, create a 24 element vector. v - 1:24 v [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 Change the dimensions to a 3-dimensional array. dim(v) - c(3,4,2) v , , 1 [,1] [,2] [,3] [,4] [1,]147 10 [2,]258 11 [3,]369 12 , , 2 [,1] [,2] [,3] [,4] [1,] 13 16 19 22 [2,] 14 17 20 23 [3,] 15 18 21 24 You can visualize a 3-dimensional array as a series of 2-dimensional arrays stacked on top of each other. But this is just a convenient image. The items are still stored consecutively in memory. Notice that layer one in the stack was filled first, and the first layer was filled just like the previous 2-dimensional example. But the items are still physically stored linearly, in consecutive locations in memory. Hope this is helpful, Dan Daniel Nordlund Bothell, WA USA __ 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. __ 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.
Re: [R] About 5.1 Arrays
On Fri, Nov 5, 2010 at 9:17 AM, Stephen Liu sati...@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 djnordl...@frontier.com To: r-help@r-project.org Sent: Fri, November 5, 2010 11:54:15 PM Subject: Re: [R] About 5.1 Arrays -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Stephen Liu Sent: Friday, November 05, 2010 7:57 AM To: Steve Lianoglou Cc: r-help@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,]147 10 [2,]258 11 [3,]369 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/ __ 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.
Re: [R] About 5.1 Arrays
Continuing with Daniel's example, but with different data values a - sample(24) a dim(a) - c(3,4,2) a as.vector(a) ## for an array with ## dim(a) == c(3,4,2) ## a[i,j,k] means select the element in position ##i + (j-1)*3 + (k-1)*3*4 index - function(i,j,k) { i + (j-1)*3 + (k-1)*3*4 } ## find the vector position described by row 2, column 1, layer 2 index(2,1,2)## this is the position in the original vector a[2,1,2]## this is the value in that position with 3D indexing a[index(2,1,2)] ## this is the same value with 1D vector indexing a[14] ## this is the same value with 1D vector indexing ## find the position in row 3, column 4, layer 1 index(3,4,1)## this is the position in the original vector a[3,4,1]## this is the value in that position with 3D indexing a[index(3,4,1)] ## this is the same value with 1D vector indexing a[12] ## this is the same value with 1D vector indexing index(1,1,1)## this is the position in the original vector index(2,1,1)## this is the position in the original vector index(3,1,1)## this is the position in the original vector index(1,2,1)## this is the position in the original vector index(2,2,1)## this is the position in the original vector index(3,2,1)## this is the position in the original vector index(1,3,1)## this is the position in the original vector index(2,3,1)## this is the position in the original vector index(3,3,1)## this is the position in the original vector index(1,4,1)## this is the position in the original vector index(2,4,1)## this is the position in the original vector index(3,4,1)## this is the position in the original vector index(1,1,2)## this is the position in the original vector index(2,1,2)## this is the position in the original vector index(3,1,2)## this is the position in the original vector index(1,2,2)## this is the position in the original vector index(2,2,2)## this is the position in the original vector index(3,2,2)## this is the position in the original vector index(1,3,2)## this is the position in the original vector index(2,3,2)## this is the position in the original vector index(3,3,2)## this is the position in the original vector index(1,4,2)## this is the position in the original vector index(2,4,2)## this is the position in the original vector index(3,4,2)## this is the position in the original vector [[alternative HTML version deleted]] __ 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.