Ma Changchen
Ma Changchen

Reputation: 115

Something wrong with using for for(){} and if(){}else{}

The follwoing code just returns the results:" 0 0 " in "d:/result.txt". I have shown the simultaneous errors that are returned below. What is wrong with my program?

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

Upvotes: 0

Views: 124

Answers (1)

rinni
rinni

Reputation: 2366

You need if(i%%5==2) as it's a comparison.

Upvotes: 7

Related Questions