Your code example doesn't work because x[3]='My 3rd choice' is not a valid named parameter assignment for a function, and that is because x[3] is not a valid name for a function argument. The _content_ of x[3] might be, but argument names aren;t parsed in this context (and indeed only would be using something like assign()).
So what R has done is say "I've found a token x[3]; that's an array dereference on x so I must evaluate that, just as I would if I encountered x[3] on its own. Now let me make sure there isn't another bracket or subset operation on that x[3]... ooh look an unexpected '=' sign. That shouldn't be there, there should only be a space, a comma or a subset operator." Other functions do exactly the same when they find an '=' in this position. If you want to use switch with variable match lists, use match in the initial expression to get an integer, and rely on integer indexing in swich() for the return value. S Ellison > -----Original Message----- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Daniel Nordlund > Sent: 24 August 2011 16:32 > To: r-help@r-project.org > Subject: Re: [R] THX-- How to use 'switch' with strings > containing spaces? > > > > > -----Original Message----- > > From: r-help-boun...@r-project.org > > [mailto:r-help-boun...@r-project.org] > > On Behalf Of Mauricio Cornejo > > Sent: Wednesday, August 24, 2011 7:53 AM > > To: Richard M. Heiberger > > Cc: r-help@r-project.org > > Subject: [R] THX-- How to use 'switch' with strings > containing spaces? > > > > Richard, > > > > Thanks for your observation and tip. > > > > My apologies that the 'expr' seemed undefined. That was > intentional > > on my part as I only wanted to show the form of the > non-working code. > > Let me be clearer by updating the code with what I actually type at > > the command line. The code below does not work (error > message included). > > > > > > x <- c("Choice 1", "Choice 2", "Choice 3") switch("Choice > 2", x[1]="My > > first choice", x[2]="My 2nd choice", x[3]="My 3rd choice") > > Error: unexpected '=' in "switch("Choice 2", x[1]=" > > > > On an earlier reply, David Winsemius suggested using the 'match' > > function instead. Perhaps that is the way to go, bypassing > 'switch' altogether. > > But I would like to know why the code above does not work. > > > > > > Again, many thanks, > > Mauricio > > > > > > Mauricio, > > I haven't seen how you are trying to use this construction, > or what you want the result to look like, so I am only > guessing. But does something like the following get you > closer to what you want? > > x <- c("Choice 1", "Choice 2", "Choice 3") switch(x[2], > 'Choice 1'="My first choice", 'Choice 2'="My 2nd choice", > 'Choice 3'="My 3rd choice") > > Hope this is helpful, > > Dan > > Daniel Nordlund > Bothell, WA USA > > ______________________________________________ > R-help@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. > ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}} ______________________________________________ R-help@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.