How to shift each row of a matrix in R

馋奶兔 提交于 2019-12-04 04:37:22

问题


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

The shifting pattern is this:

shift by 0 for row 1
shift by 1 for row 2
shift by 2 for row 3
...
shift by n-1 for row n

This can be done with a for loop of course. I am wondering if there is a better way?


回答1:


Assuming that your example is representative, i.e., you have always a triangle structure of letters and zeros:

mat <- structure(c("a", "d", "f", "b", "e", "0", "c", "0", "0"), 
                 .Dim = c(3L, 3L), .Dimnames = list(NULL, NULL))
res <- matrix(0, nrow(mat), ncol(mat))
res[lower.tri(res, diag=TRUE)] <- t(mat)[t(mat)!="0"]
t(res)
#     [,1] [,2] [,3]
# [1,] "a"  "b"  "c" 
# [2,] "0"  "d"  "e" 
# [3,] "0"  "0"  "f" 



回答2:


A head and tail solution doesn't seem as readable as a for loop to me, and may not even be as quick. Nonetheless...

t( sapply( 0:(nrow(mat)-1) , function(x) c( tail( mat[x+1,] , x ) , head( mat[x+1,] , nrow(mat)-x ) ) ) )
#     [,1] [,2] [,3]
#[1,] "a"  "b"  "c" 
#[2,] "0"  "d"  "e" 
#[3,] "0"  "0"  "f" 

A for loop version of this could be...

n <- nrow(mat)
for( i in 1:n ){
    mat[i,] <- c( tail( mat[i,] , i-1 ) , head( mat[i,] , n-(i-1)  ) )
}



回答3:


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


来源:https://stackoverflow.com/questions/24143992/how-to-shift-each-row-of-a-matrix-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!