[R] switch() usage

Michael A. Gilchrist mikeg at utk.edu
Wed Aug 27 03:02:30 CEST 2008


Hello,

I am wondering if it is possible to run separate lines of code within switch() 
as one can do in most languages.

For example, I'd like to have switch execute a few lines of code based on the 
value passed to it.

For example,

>index = 1
>
>switch(index,
>	command 1; command 2; command 3, #execute if index = 1
>	command 4; command 5; command 6, #execute if index = 2
>	command 7....                    #execute if index = 3
>
>)

However, if I try this syntax, I get an error about an unexpected ';'. I've 
tried wrapping the commands in () but that doesn't help either.

Is there a way to do this without having to  write separate functions for each 
of the switch possibilities.

Thanks.


Mike


-----------------------------------------------------
Department of Ecology & Evolutionary Biology
569 Dabney Hall
University of Tennessee
Knoxville, TN 37996-1610

phone:(865) 974-6453
fax:  (865) 974-6042

web: http://eeb.bio.utk.edu/gilchrist.asp



More information about the R-help mailing list