Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中的向量中包含变量?_R_Loops_Variables - Fatal编程技术网

如何在R中的向量中包含变量?

如何在R中的向量中包含变量?,r,loops,variables,R,Loops,Variables,在循环中,我想在向量中插入“I”。我该怎么做 我尝试了以下代码: m1nash.best.response.coordinates<- NULL for (i in 1:2) { if(m1nash[1,i]>m1nash[2,i]) { m1nash.best.response.coordinates <- c(m1nash.best.response.coordinates, 1,i) } if(m1nash[2,i]>m1nash[1,i]) {

在循环中,我想在向量中插入“I”。我该怎么做

我尝试了以下代码:

m1nash.best.response.coordinates<- NULL
for (i in 1:2) {
  if(m1nash[1,i]>m1nash[2,i]) {
    m1nash.best.response.coordinates <- c(m1nash.best.response.coordinates, 1,i)
  } if(m1nash[2,i]>m1nash[1,i]) {
    m1nash.best.response.coordinates <- c(m1nash.best.response.coordinates, 2, i)
  }

}
m1nash.best.response.Coordinates1nash[2,i]){
m1nash.best.response.com(m1nash[1,i]){
m1nash.best.response.coordinates m1nash.best.response.coordinates}
错误:“}”中出现意外“}”
>   
> }
错误:“}”中出现意外“}”
试试这个:

m1nash.best.response.coordinates<- NULL
for (i in 1:2) {
  if (m1nash[1,i]>m1nash[2,i]) {
    m1nash.best.response.coordinates <- c(m1nash.best.response.coordinates, 1,i)
  } 
  if (m1nash[2,i]>m1nash[1,i]) {
    m1nash.best.response.coordinates <- c(m1nash.best.response.coordinates, 2, i)
  }

}
m1nash.best.response.Coordinates1nash[2,i]){
m1nash.best.response.com(m1nash[1,i]){
m1nash.best.response.coordinates欢迎Kaan

您应该做出几个最佳实践选择,包括
for
循环还是
if
语句是最佳选择

然而,如果您所担心的只是让代码正常工作,那么您可以做一些事情

我更愿意将
m1nash.best.response.coordinates
初始化为空向量,而不是
NULL
。如果可以,请将其指定为
c()

其次,我相信你在这一行有拼写错误
if(m1nash[2,I]>m1nas[1,I])
m1nas
应该是
m1nash

您可以尝试下面的循环

m1nash.best.response.coordinates <- c()
m1nash <- matrix(data = rnorm(4), nrow=2, ncol=2) #my test matrix

for (i in 1:2) {
  if (m1nash[1, i] > m1nash[2, i]) {
    m1nash.best.response.coordinates <-
      c(m1nash.best.response.coordinates, 1, i)
  }
  if (m1nash[2, i] > m1nash[1, i]) {
    m1nash.best.response.coordinates <-
      c(m1nash.best.response.coordinates, 2, i)
  }

}

R中的
m1nash.best.response.coordinates,“空向量”和
NULL
几乎可以互换。使用
c()初始化绝对不重要
而不是
NULL
。我以为我编辑了拼写错误,但还是感谢您指出了它。我们循环之间唯一的区别是您将第二个if语句放在了一行,而不是}.当我进行同样的修改时,代码起了作用。现在将重点放在两列相等的部分。非常感谢!是的,我认为这是一种习惯,但是@KonradRudolph用换行符的注释击中了要害。这是最主要的。如果你计划为更大的矩阵或更大的循环运行类似类型的代码,你可能不得不这样做重新设计您的整个流程。现在,一旦您开始缩放,代码将非常低效。祝您好运!@robelbaker一开始我喜欢在较小的示例上演示我的想法,然后再进行缩放。但您似乎有一个观点。在R中,语句由换行符分隔,一行中只能出现一条语句(除非用分号分隔-但不要这样做)。因此出现了错误。奇怪的是,如果m1nash呢?为什么只
1:2
?您可能不需要任何循环。我打算在一个小示例(2*2支付矩阵)上编写这样一个使用可概化技术(循环)的代码。
m1nash.best.response.coordinates <- c()
m1nash <- matrix(data = rnorm(4), nrow=2, ncol=2) #my test matrix

for (i in 1:2) {
  if (m1nash[1, i] > m1nash[2, i]) {
    m1nash.best.response.coordinates <-
      c(m1nash.best.response.coordinates, 1, i)
  }
  if (m1nash[2, i] > m1nash[1, i]) {
    m1nash.best.response.coordinates <-
      c(m1nash.best.response.coordinates, 2, i)
  }

}