Dear all, Possibly a rudimentary question, however any help is greatly appreciated. I am sorting a large matrix into an array of dim(p(i),q,3). I put each entry into a corresponding matrix (1 of the 3) based on some criteria. I figure this will assist me in condensing code as I can loop through the 3rd dimension of the array instead of generating 3 separate matrices and using the same block of code 3 times. My question is how to get the colnames of the 3 nested matrices in the array to match the colnames of the data matrix. In other words... DATA: Exp region Qty Ct ...q 1 S CB 3.55 2.15 . 2 S TG 4.16 2.18 . 3 C OO 2.36 3.65 . 4 C . . . . . . . . . . . . . . . . . . p ........................... ARRAY 1 [,1] [,2] [,3] [,4]...q 1 SOME DATA WILL FILL THIS . 2 . . . . 3 . . . . 4 . . . . . . . . . . . . . . . . . . . P(1) ........................... 2 [,1] [,2] [,3] [,4]...q 1 SOME DATA WILL FILL THIS . 2 . . . . 3 . . . . 4 . . . . . . . . . . . . . . . . . . . P(2) ........................... 3 [,1] [,2] [,3] [,4]...q 1 SOME DATA WILL FILL THIS . 2 . . . . 3 . . . . 4 . . . . . . . . . . . . . . . . . . . P(3) ........................... Again, how to get those [,1], [,2]... to read (and operate) in the same fashion as the column names in the data matrix? Also, am I interpreting the dimensions of the array incorrectly? Please feel free to post any helpful links on the subject, as I have found "dimnames" and "array" in the R-help documentation unhelpful. Any help is greatly appreciated. Dave Mitchell Undergraduate: Statistics and Mathematics, University of Illinois, Urbana-Champaign [[alternative HTML version deleted]]
dave mitchell wrote:> Dear all, > Possibly a rudimentary question, however any help is greatly appreciated. I > am sorting a large matrix into an array of dim(p(i),q,3). I put each entry > into a corresponding matrix (1 of the 3) based on some criteria. I figure > this will assist me in condensing code as I can loop through the 3rd > dimension of the array instead of generating 3 separate matrices and using > the same block of code 3 times. My question is how to get the colnames of > the 3 nested matrices in the array to match the colnames of the data > matrix. In other words... >Denoting with array3d the array and with matrix2d the data matrix: colnames(array3d)=colnames(array3d) Otherwise, using dimnames: dimnames(array3d)=list(NULL,colnames(array3d),NULL) You can operate using the "[" operator, that is: array3d[,"region",] to extract the region columns from each dimension. You obtain the same effect using the column number, i.e. array3d[,2,] domenico> DATA: > Exp region Qty Ct ...q > 1 S CB 3.55 2.15 . > 2 S TG 4.16 2.18 . > 3 C OO 2.36 3.65 . > 4 C . . . > . . . . . > . . . . . > . . . . . > p ........................... > > > > ARRAY > 1 > [,1] [,2] [,3] [,4]...q > 1 SOME DATA WILL FILL THIS . > 2 . . . . > 3 . . . . > 4 . . . . > . . . . . > . . . . . > . . . . . > P(1) ........................... > > 2 > [,1] [,2] [,3] [,4]...q > 1 SOME DATA WILL FILL THIS . > 2 . . . . > 3 . . . . > 4 . . . . > . . . . . > . . . . . > . . . . . > P(2) ........................... > 3 > [,1] [,2] [,3] [,4]...q > 1 SOME DATA WILL FILL THIS . > 2 . . . . > 3 . . . . > 4 . . . . > . . . . . > . . . . . > . . . . . > P(3) ........................... > > Again, how to get those [,1], [,2]... to read (and operate) in the same > fashion as the column names in the data matrix? Also, am I interpreting the > dimensions of the array incorrectly? Please feel free to post any helpful > links on the subject, as I have found "dimnames" and "array" in the R-help > documentation unhelpful. Any help is greatly appreciated. > > Dave Mitchell > Undergraduate: Statistics and Mathematics, > University of Illinois, Urbana-Champaign > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. > >
Sorry, there were mistakes in variable names... (I realized only after pressed the send button) Domenico Vistocco wrote:> dave mitchell wrote: > >> Dear all, >> Possibly a rudimentary question, however any help is greatly appreciated. I >> am sorting a large matrix into an array of dim(p(i),q,3). I put each entry >> into a corresponding matrix (1 of the 3) based on some criteria. I figure >> this will assist me in condensing code as I can loop through the 3rd >> dimension of the array instead of generating 3 separate matrices and using >> the same block of code 3 times. My question is how to get the colnames of >> the 3 nested matrices in the array to match the colnames of the data >> matrix. In other words... >> >> > Denoting with array3d the array and with matrix2d the data matrix: > > colnames(array3d)=colnames(array3d) >colnames(array3d)=colnames(matrix2d)> Otherwise, using dimnames: > dimnames(array3d)=list(NULL,colnames(array3d),NULL) >dimnames(array3d)=list(NULL,colnames(matrix2d),NULL)> You can operate using the "[" operator, that is: > array3d[,"region",] to extract the region columns from each dimension. > > You obtain the same effect using the column number, i.e. > array3d[,2,] > > domenico > >> DATA: >> Exp region Qty Ct ...q >> 1 S CB 3.55 2.15 . >> 2 S TG 4.16 2.18 . >> 3 C OO 2.36 3.65 . >> 4 C . . . >> . . . . . >> . . . . . >> . . . . . >> p ........................... >> >> >> >> ARRAY >> 1 >> [,1] [,2] [,3] [,4]...q >> 1 SOME DATA WILL FILL THIS . >> 2 . . . . >> 3 . . . . >> 4 . . . . >> . . . . . >> . . . . . >> . . . . . >> P(1) ........................... >> >> 2 >> [,1] [,2] [,3] [,4]...q >> 1 SOME DATA WILL FILL THIS . >> 2 . . . . >> 3 . . . . >> 4 . . . . >> . . . . . >> . . . . . >> . . . . . >> P(2) ........................... >> 3 >> [,1] [,2] [,3] [,4]...q >> 1 SOME DATA WILL FILL THIS . >> 2 . . . . >> 3 . . . . >> 4 . . . . >> . . . . . >> . . . . . >> . . . . . >> P(3) ........................... >> >> Again, how to get those [,1], [,2]... to read (and operate) in the same >> fashion as the column names in the data matrix? Also, am I interpreting the >> dimensions of the array incorrectly? Please feel free to post any helpful >> links on the subject, as I have found "dimnames" and "array" in the R-help >> documentation unhelpful. Any help is greatly appreciated. >> >> Dave Mitchell >> Undergraduate: Statistics and Mathematics, >> University of Illinois, Urbana-Champaign >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> 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. >> >> >> > > ______________________________________________ > 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. > >
I can't quite understand what you're having difficulty with (is it constructing the array, or coping with the different 'matrices' having different column names, or something else?) However, your sample data looks like it has a mixture of factor (region) and numeric data (Qty), so you're probably storing it in a data frame. AFAIK, there is no 3d object in R that can store mixed-type data like a data frame can. An array object in R has to have the same data type for every column etc. -- Tony Plate dave mitchell wrote:> Dear all, > Possibly a rudimentary question, however any help is greatly appreciated. I > am sorting a large matrix into an array of dim(p(i),q,3). I put each entry > into a corresponding matrix (1 of the 3) based on some criteria. I figure > this will assist me in condensing code as I can loop through the 3rd > dimension of the array instead of generating 3 separate matrices and using > the same block of code 3 times. My question is how to get the colnames of > the 3 nested matrices in the array to match the colnames of the data > matrix. In other words... > > DATA: > Exp region Qty Ct ...q > 1 S CB 3.55 2.15 . > 2 S TG 4.16 2.18 . > 3 C OO 2.36 3.65 . > 4 C . . . > . . . . . > . . . . . > . . . . . > p ........................... > > > > ARRAY > 1 > [,1] [,2] [,3] [,4]...q > 1 SOME DATA WILL FILL THIS . > 2 . . . . > 3 . . . . > 4 . . . . > . . . . . > . . . . . > . . . . . > P(1) ........................... > > 2 > [,1] [,2] [,3] [,4]...q > 1 SOME DATA WILL FILL THIS . > 2 . . . . > 3 . . . . > 4 . . . . > . . . . . > . . . . . > . . . . . > P(2) ........................... > 3 > [,1] [,2] [,3] [,4]...q > 1 SOME DATA WILL FILL THIS . > 2 . . . . > 3 . . . . > 4 . . . . > . . . . . > . . . . . > . . . . . > P(3) ........................... > > Again, how to get those [,1], [,2]... to read (and operate) in the same > fashion as the column names in the data matrix? Also, am I interpreting the > dimensions of the array incorrectly? Please feel free to post any helpful > links on the subject, as I have found "dimnames" and "array" in the R-help > documentation unhelpful. Any help is greatly appreciated. > > Dave Mitchell > Undergraduate: Statistics and Mathematics, > University of Illinois, Urbana-Champaign > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >