[R] naming rows/columns in 'array of matrices'
S Ellison
S.Ellison at LGCGroup.com
Mon Feb 2 16:09:24 CET 2015
> > 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}}
More information about the R-help
mailing list