[R] switch() usage
Michael A. Gilchrist
mikeg at utk.edu
Wed Aug 27 03:18:27 CEST 2008
Hi Dan,
No, I hadn't thought of that and it WORKS!
I was hoping there'd be such a simple solution. I have new respect for the
power of {} w/in R.
Many 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
-----------------------------------------------------
On Tue, 26 Aug 2008, Nordlund, Dan (DSHS/RDA) wrote:
>> -----Original Message-----
>> From: r-help-bounces at r-project.org
>> [mailto:r-help-bounces at r-project.org] On Behalf Of Michael A.
>> Gilchrist
>> Sent: Tuesday, August 26, 2008 6:03 PM
>> To: r-help at r-project.org
>> Subject: [R] switch() usage
>>
>> 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
>>
>>
> Mike,
>
> Did you try wrapping the series of commands in braces {}:
>
> 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
> )
>
> Hope this is helpful,
>
> Dan
>
> Daniel J. Nordlund
> Washington State Department of Social and Health Services
> Planning, Performance, and Accountability
> Research and Data Analysis Division
> Olympia, WA 98504-5204
>
>
>
> ______________________________________________
> 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