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 ________________________________ From: Richard M. Heiberger <r...@temple.edu> Cc: "r-help@r-project.org" <r-help@r-project.org> Sent: Tuesday, August 16, 2011 6:59 PM Subject: Re: [R] How to use 'switch' with strings containing spaces? The problem is that your argument expr is undefined. This works > switch("Choice 2", "Choice 1"="My first choice", "Choice 2"="My 2nd choice", > "Choice 3"="My 3rd choice") [1] "My 2nd choice" > x <- "Choice 2" > switch(x, "Choice 1"="My first choice", "Choice 2"="My 2nd choice", "Choice > 3"="My 3rd choice") [1] "My 2nd choice" > On Tue, Aug 16, 2011 at 4:53 PM, Mauricio Cornejo <mauriciocorn...@yahoo.com> wrote: Hi, > >Does anyone know if the alternatives in the 'switch' function can be specified >as strings containing spaces? Neither of the two approaches below works. > > >switch(expr, "Choice 1"="My first choice", "Choice 2"="My 2nd choice", "Choice >3"="My 3rd choice") > > >x <- c("Choice 1", "Choice 2", "Choice 3") >switch(expr, x[1]="My first choice", x[2]="My 2nd choice", x[3]="My 3rd >choice") > > >If the intended functionality can be achieved by a different function(s), I'd >most appreciate such a suggestion as well. > >Many thanks, >Mauricio > > [[alternative HTML version deleted]] > > >______________________________________________ >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. > > [[alternative HTML version deleted]]
______________________________________________ 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.