在for循环中引用R中矩阵的条目 [英] Referencing entries of matrix in R within for loop

查看:119
本文介绍了在for循环中引用R中矩阵的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在for循环中引用矩阵的一部分?

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]      
   }
}

我收到一条错误消息,提示"alldata.t1 [j,8k]中出现意外的符号.执行这种操作的正确语法是什么?

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

谢谢.

推荐答案

使用括号& *相乘:

use parens & * to multiply:

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


seq运算符:优先于算术运算符,例如- 因此,在没有括号的情况下,您拥有


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

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

这篇关于在for循环中引用R中矩阵的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆