使用for for(){}和if(){} else {}有问题

Ma *_*hen 0 for-loop if-statement r

下面的代码只返回结果:"d:/result.txt"中的"0 0".我已经显示了下面返回的同时错误.我的计划有什么问题?

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 "}"
Run Code Online (Sandbox Code Playgroud)

rin*_*nni 7

你需要if(i%%5==2)它作为比较.