[R] naming rows/columns in 'array of matrices'

Evan Cooch evan.cooch at gmail.com
Fri Jan 30 20:07:34 CET 2015


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!



More information about the R-help mailing list