[R] Odp: If Else Function

Petr PIKAL petr.pikal at precheza.cz
Tue Dec 11 13:47:02 CET 2007


Hi

r-help-bounces at r-project.org napsal dne 11.12.2007 13:06:14:

> 
> Hello everyone, 
> 
> I would like to use the "if" statements and I was thinking sth like
> 
> If () {} else{ if() {} else{} } 
> 
> Is this a possible solution and is there any syntax error?

Except the first letter, nothing sooms to be wrong.

> if (1==1) {print("a")} else { if(1==1) {print("b")} else{print("c")} } 
[1] "a"
> if (1==2) {print("a")} else { if(1==1) {print("b")} else{print("c")} } 
[1] "b"
> if (1==2) {print("a")} else { if(1==2) {print("b")} else{print("c")} } 
[1] "c"
>

I beleive you noticed a difference between if() and ifelse() functions. 
And of course if you spread your command in several rows be carefull not 
to have valid syntax before you ended a whole command.
e.g.
if (1==2) {print("a")} 
else { if(1==2) {print("b")} else{print("c")} }

Error: unexpected 'else' in "else"


Regards
Petr


> 
> Could anyone give me some advice? Many thanks.
> -- 
> View this message in context: http://www.nabble.com/If-Else-Function-
> tp14272459p14272459.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list