2012-09-10 37 views
0

以下代码只是返回结果:“0 0”在“d:/result.txt”中。 我已经显示下面返回的同时发生的错误。我的程序有什么问题?使用for(){}和if(){} else else {}

for (i in 2:31) 
{ 
if(i%%5=2) 
{cat(i,1,"\n",append=TRUE,file="d:/result.txt")} 
else{cat(0,0,"\n",append=TRUE,file="d:/result.txt")} 
} 

> for (i in 2:31) 
+ { 
+ if(i%%5=2){ 
Error: unexpected '=' in: 
"{ 
if(i%%5=" 
> cat(i,1,"\n",append=TRUE,file="d:/result.txt") 
Error in cat(i, 1, "\n", append = TRUE, file = "d:/result.txt") : 
object 'i' not found 
> } 
Error: unexpected '}' in "}" 
> else { 
Error: unexpected 'else' in "else" 
>  cat(0,0,"\n",append=TRUE,file="d:/result.txt") 
> } 
Error: unexpected '}' in "}" 
> } 
Error: unexpected '}' in "}" 
+2

使用'=='比较,即'if(i %% 5 == 2)'。 '='用于赋值。 – Backlin

回答

7

您需要if(i%%5==2),因为这是一个比较。

+0

非常感谢您。非常感谢。 –