[R] How "else" works

Deepayan Sarkar deepayan at stat.wisc.edu
Wed Feb 12 00:18:03 CET 2003


On Tuesday 11 February 2003 04:40 pm, Brian.J.GREGOR at odot.state.or.us wrote:
> I have what is likely to be a simple question about the else keyword.
>
> The usage in the help pages is as follows:
>
> 	if(cond) cons.expr  else  alt.expr
>
> I would expect to be able to use it in the following way as well:
>
> 	if(cond){
> 		cons.expr
> 	}
> 	else alt.expr
>
> This results a syntax error.


What makes you say it does not work ? For example,


> foo = function(x) {
+    if (x == 1) {
+        print("one")
+    }
+    else {
+        print(x)
+    }
+  }
> 
> foo(1)
[1] "one"
> foo(2)
[1] 2
>




More information about the R-help mailing list