功能错误 - 替换长度为零

时间:2017-02-06 06:22:07

标签: r

我想创建一个矩阵,其中第一列从0开始,每个后续行减去2.(0,-2,-4,-6等)矩阵的维数取决于函数alignment_matrix的参数中两个向量的长度。

alignment_matrix <- function(seq1, seq2){
  M <- matrix(0, nrow = length(seq1)+1, ncol = length(seq2)+1)
    for(i in 1:(nrow(M))-1){
    M[i+1,1] <- M[i,1] - 2
  } 
  M
}

当seq1和seq2如下:

seq1 <- c('G','A','A','T','T','C')
seq2 <- c('G','A','T','T','A')

所需的结果矩阵应如下所示:

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    0    0    0    0    0    0
[2,]   -2    0    0    0    0    0
[3,]   -4    0    0    0    0    0
[4,]   -6    0    0    0    0    0
[5,]   -8    0    0    0    0    0
[6,]  -10    0    0    0    0    0
[7,]  -12    0    0    0    0    0

但是,当我运行该函数时,我收到以下错误消息:

Error in M[i + 1, 1] <- M[i, 1] - 2 : replacement has length zero

0 个答案:

没有答案