[R] IF/Else

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Thu Nov 10 23:45:05 CET 2005


On 10-Nov-05 Guenther, Cameron wrote:
> Hi,
> I am trying to write a for loop with if else statements to calculate
> biomass density estimates for different types of sampling gear.  
> My code is:
> 
> bmd=for (i in 1:length(Gear)){
> if (Gear==20) {bioden=Biomass/141}
> else {if (Gear==23) {bioden=Biomass/68}}
> else {if (Gear==160) {bioden=Biomass/4120}}
> else {if (Gear==170) {bioden=Biomass/2210}}
> else {if (Gear==300) {bioden=Biomass/(DIST_TOW*4*1853)}}
> else {if (Gear==301) {bioden=Biomass/(DIST_TOW*4*1853)}}
> }

In the above, you have in effect written a string of "else"s
following a single if, since the {if(Gear==160){...}} isolates
its "if" from the preceding else, etc.. But there are other
things wrong.

The following should work:

  bmd=for (i in 1:length(Gear)) {
   if (Gear==20) {bioden=Biomass/141}  else 
    if (Gear==23) {bioden=Biomass/68}   else 
     if (Gear==160) {bioden=Biomass/4120}  else 
      if (Gear==170) {bioden=Biomass/2210}  else 
       if (Gear==300) {bioden=Biomass/(DIST_TOW*4*1853)}  else 
        if (Gear==301) {bioden=Biomass/(DIST_TOW*4*1853)}
  }

where I have removed superfluous "{" on the left and "}" on the
right.

Putting the "else" at the end of the line, rather than on the
next, is needed since otherwise the line is a completed
statement befor the "else" is encountered and then the "else"
is a syntax error. With "else" at the end of the line, the
end of line is reached on an incomplete statement so R goes
on to the next line before making up its mind about it.

I.e. Good:

  A<-2
  if(A==1){B<-1} else
    if(A==2){B<-2} else
       if(A==3){B<-3}

while Bad:
  A<-2
  if(A==1){B<-1}
  else if(A==2){B<-2}
       else if(A==3){B<-3}

Also Bad:
  A<-2
  if(A==1){B<-1} else
    {if(A==2){B<-2}} else
       {if(A==3){B<-3}}

since each "{if(A==2){B<-2}}" is a complete statement delimited
by "{...}" and the "else" is then again a syntax error, The final
statement is OK, however.

Mind you, if the cases in your code are the only possibilites
for Gear, then you don't need the final "if" at all -- just the
assignment, since if you get that far it's the only case left,
and you'll only reach it by falling through the preceding "else".

Indeed, you could even dispense with all of the "else"s and
just use "if" statements, since only one of them will be
satisfied; but of course that is a bit inefficient in that
every statement will be tested even if one has already been
satisfied.

Hoping this helps,
Ted.

> The syntax that is returned is:
> 
>> bmd=for (i in 1:length(Gear)){
> + if (Gear==20) {bioden=Biomass/141}
> + else {if (Gear==23) {bioden=Biomass/68}}
> + else {if (Gear==160) {bioden=Biomass/4120}}
> Error: syntax error in:
> "else {if (Gear==23) {bioden=Biomass/68}}
> else"
>> else {if (Gear==170) {bioden=Biomass/2210}}
> Error: syntax error in "else"
>> else {if (Gear==300) {bioden=Biomass/(DIST_TOW*4*1853)}}
> Error: syntax error in "else"
>> else {if (Gear==301) {bioden=Biomass/(DIST_TOW*4*1853)}}
> Error: syntax error in "else"
>> }
> Error: syntax error in "}"
> 
> It appears that the code works for the first two if/else statements and
> then fails there after.  Any suggestions?

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 10-Nov-05                                       Time: 22:44:59
------------------------------ XFMail ------------------------------




More information about the R-help mailing list