I have a matrix of this form:
a b c
d e 0
f 0 0
and I want to transform it into something like this:
a b c
0 d e
0 0 f
I think this is all you need:
mat<-matrix(1:25,5)
mat
[,1] [,2] [,3] [,4] [,5]
[1,] 1 6 11 16 21
[2,] 2 7 12 17 22
[3,] 3 8 13 18 23
[4,] 4 9 14 19 24
[5,] 5 10 15 20 25
for(j in 2:nrow(mat) ) mat[j,]<-mat[j, c(j:ncol(mat),1:(j-1))]
mat
[,1] [,2] [,3] [,4] [,5]
[1,] 1 6 11 16 21
[2,] 7 12 17 22 2
[3,] 13 18 23 3 8
[4,] 19 24 4 9 14
[5,] 25 5 10 15 20