[R] Matrix interesting question!

David Winsemius dwinsemius at comcast.net
Fri May 28 20:21:11 CEST 2010


On May 28, 2010, at 12:30 PM, UM wrote:

>
> hi,
> I have been trying to do this in R (have implemented it in Excel)  
> but I have
> been using a very inefficent way (loops etc.). I have matrix A  
> (columns are
> years and ages are rows)  and matrix B (columns are birth yrs and  
> rows are
> ages)
>
>
> I would like to first turn matrix A into matrix B
>

 > catrow <- function(A, rn) c(rep("0", 3-rn), A[rn,],rep("0", 
(2+rn)-3) )
 > matrix(sapply(1:3, function(x) catrow(A, x)) , ncol=2*ncol(A)-1,  
byrow=TRUE)
      [,1] [,2] [,3] [,4] [,5]
[1,] "0"  "0"  "a"  "b"  "c"
[2,] "0"  "d"  "e"  "f"  "0"
[3,] "g"  "h"  "i"  "0"  "0"

> And then I would like to convert matrix B back again to the original  
> matrix
> A.

Left as an exercise for the reader.

> (I have left out details of steps) but this is the gist of what I  
> want to
> do. Can anyone please give any insights?


David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list