Suppose I have the following situation: I have an array of 2 matrices, where each matrix is (2x2): P <- array(0, c(2,2,2)) P[,,1] <- matrix(c(1,2,3,4),2,2,byrow=T); P[,,2] <- matrix(c(5,6,7,8),2,2,byrow=T); I want to label rows and columns of each matrix in the array, such that P would look like live dead live 1 2 dead 3 4 , , 2 live dead live 5 6 dead 7 8 I've tried 'direct, brute force" approaches like rownames(P[,,1]) <- c("live","dead") colnames(P[,,1]) <- c("live","dead") (repeated for the second matrix), but this doesn't work. Since all of the matrices are of the same dimension(s), and since I want the same rownames and colnames for each matrix, I'm hoping there is some simply magical permutation of lapply (I'm guessing) which will do the trick. I'd also be interested in why the 'direct, brute force' approach (above) doesn't work, and what does, since I might need to manipulate row/col names for individual matrices in the array (if, say, dimensions of the matrices were not the same over the array). Thanks in advance!
The (obvious, after the fact) solution at the bottom. D'oh... On 1/30/2015 2:07 PM, Evan Cooch wrote:> Suppose I have the following situation: > > I have an array of 2 matrices, where each matrix is (2x2): > > P <- array(0, c(2,2,2)) > > P[,,1] <- matrix(c(1,2,3,4),2,2,byrow=T); > P[,,2] <- matrix(c(5,6,7,8),2,2,byrow=T); > > I want to label rows and columns of each matrix in the array, such > that P would look like > > > live dead > live 1 2 > dead 3 4 > > , , 2 > > live dead > live 5 6 > dead 7 8 > > I've tried 'direct, brute force" approaches like > > rownames(P[,,1]) <- c("live","dead") > colnames(P[,,1]) <- c("live","dead") > > (repeated for the second matrix), but this doesn't work. > > Since all of the matrices are of the same dimension(s), and since I > want the same rownames and colnames for each matrix, I'm hoping there > is some simply magical permutation of lapply (I'm guessing) which will > do the trick.Forgot I was dealing with a multi-dimensional array, not a list. So, following works fine. I'm sure there are better approaches (where 'better' is either 'cooler', or 'more flexible'), but for the moment...) P <- array(0, c(2,2,2),dimnames=list(c("live","dead"),c("old","young"),NULL)) P[,,1] <- matrix(c(1,2,3,4),2,2,byrow=T); P[,,2] <- matrix(c(5,6,7,8),2,2,byrow=T); print(P);
peter dalgaard
2015-Jan-31 08:19 UTC
[R] naming rows/columns in 'array of matrices' | solved
> On 30 Jan 2015, at 20:34 , Evan Cooch <evan.cooch at gmail.com> wrote: > > The (obvious, after the fact) solution at the bottom. D'oh... >[snip]> Forgot I was dealing with a multi-dimensional array, not a list. So, following works fine. I'm sure there are better approaches (where 'better' is either 'cooler', or 'more flexible'), but for the moment...) > > P <- array(0, c(2,2,2),dimnames=list(c("live","dead"),c("old","young"),NULL)) > > P[,,1] <- matrix(c(1,2,3,4),2,2,byrow=T); > P[,,2] <- matrix(c(5,6,7,8),2,2,byrow=T); > > print(P); >Just for completeness, this also works:> P <- array(0, c(2,2,2)) > P[,,1] <- matrix(c(1,2,3,4),2,2,byrow=T); > P[,,2] <- matrix(c(5,6,7,8),2,2,byrow=T);> dimnames(P)[[1]] <- c("live","dead") > dimnames(P)[[2]] <- c("live","dead")-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
In your example, P is a three dimensional array. You can assign names to the three dimensions using the dimnames() function. For example, this command assigns names to the first two dimensions, but leaves the third dimension without names. dimnames(P) <- list(c("live", "dead"), c("live", "dead"), NULL) Jean On Fri, Jan 30, 2015 at 1:07 PM, Evan Cooch <evan.cooch at gmail.com> wrote:> Suppose I have the following situation: > > I have an array of 2 matrices, where each matrix is (2x2): > > P <- array(0, c(2,2,2)) > > P[,,1] <- matrix(c(1,2,3,4),2,2,byrow=T); > P[,,2] <- matrix(c(5,6,7,8),2,2,byrow=T); > > I want to label rows and columns of each matrix in the array, such that P > would look like > > > live dead > live 1 2 > dead 3 4 > > , , 2 > > live dead > live 5 6 > dead 7 8 > > I've tried 'direct, brute force" approaches like > > rownames(P[,,1]) <- c("live","dead") > colnames(P[,,1]) <- c("live","dead") > > (repeated for the second matrix), but this doesn't work. > > Since all of the matrices are of the same dimension(s), and since I want > the same rownames and colnames for each matrix, I'm hoping there is some > simply magical permutation of lapply (I'm guessing) which will do the trick. > > I'd also be interested in why the 'direct, brute force' approach (above) > doesn't work, and what does, since I might need to manipulate row/col names > for individual matrices in the array (if, say, dimensions of the matrices > were not the same over the array). > > Thanks in advance! > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
> > I'd also be interested in why the 'direct, brute force' approach > > (above) doesn't work,Your example was a 3-dimensional array, so> rownames(P) <- colnames(P) <- c(live', 'dead')would have worked; rownames() and colnames() work on dimnames[1] and dimnames[2]. But rownames(P[,,1]) could not have worked, because you were not assigning to the names of P; you were assigning to something (P[,,1]) extracted from P. In effect, you were doing the equivalent of {P1 <- P[,,1] rownames(P1) <- c('live', 'dead') rm(P1)}> > ... since I might need to manipulate > > row/col names for individual matrices in the array (if, say, > > dimensions of the matrices were not the same over the array).If the dimension are different you will not have an array; you'd have to have a list of matrices. You could then use lapply for the one constant dimension size; for example, lp <- list(P22 = matrix(ncol=2, nrow=2), P32= matrix(ncol=2, nrow=3)) lapply(lp, function(x, cn=c('live', 'dead')) {colnames(x)<-cn; x}) Other than that, you'd either have to do some careful conditional coding or apply names manually. S ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}