【发布时间】:2019-08-31 06:19:29
【问题描述】:
在循环中,我想将“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)
}
}
遇到以下控制台:
Error: unexpected 'if' in:
" m1nash.best.response.coordinates <- c(m1nash.best.response.coordinates, 1,i)
} if"
> m1nash.best.response.coordinates <- c(m1nash.best.response.coordinates, 2, i)
Error: object 'i' not found
> }
Error: unexpected '}' in " }"
>
> }
Error: unexpected '}' in "}"
【问题讨论】:
-
在 R 中,语句由换行符分隔,并且一行中只能出现一个语句(除非用分号分隔 - 但不要这样做)。因此出现错误。
-
很好奇,如果 m1nash 会怎样?为什么只有
1:2?您可能不需要任何循环。 -
我打算在一个小例子(一个 2*2 收益矩阵)上编写这样一个使用泛化技术(循环)的代码。