[R] Matrix diagonal help

Usman Munir
Fri, 23 Apr 2010 09:45:54 -0000

Hi

Suppose I have a matrix (cohort are rows and years are columns)



[2000]  [2001]  [2002]  [2003]

[C1]     0.01     0.03     0.02     0.09

[C2]     0.06     0.05     0.07     0.11

[C3]     0.1       0.5       0.4       0.98

[C4]     0.7       0.6       0.2       0.77





I want to extracts the diagonals to get a matrix which looks like this (C1
becomes C2 in 2002, C2 becomes C3 in 2003 and so on)



[C1]     [C2]     [C3]     [C4]

[2000]  0.01     0.06     0.1       0.7

[2001]  0.05     0.5       0.6       0

[2002]  0.4       0.2       0          0

[2003]  0.77     0          0          0



The matrix can be of any size (for simplicity I have created a 4*4). Is
there a function in R that does this? I know R has functions like R diag and
lower triangular but that doesn’t help. Any tips will be appreciated.


Thanks!

	[[alternative HTML version deleted]]