Referencing entries of matrix in R within for loop

你说的曾经没有我的故事 提交于 2020-01-17 03:03:31

问题


Is there a way to reference a part of a matrix within a for loop?

for (j in 1:x1)
  for (k in 1:x2) {
    matrix[j,8k-6:8k+1] <- AlleleFreq.t1[k,1:8]      
   }
}

I get an error message saying "unexpected symbol in "alldata.t1[j,8k". What is the correct syntax for preforming this sort of operation?

Thank you.


回答1:


use parens & * to multiply:

 8k-6:8k+1   ~~~>  (8*k-6):(8*k+1)

the seq operator : takes precedence over arithmetic operators such as - Thus, without parens, you have

(8*k) - c(6, 7, 8) + ((8*k) + 1)


来源:https://stackoverflow.com/questions/20553057/referencing-entries-of-matrix-in-r-within-for-loop

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