Mauricio Cornejo
2011-Aug-16 20:53 UTC
[R] How to use 'switch' with strings containing spaces?
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]]
David Winsemius
2011-Aug-16 22:52 UTC
[R] How to use 'switch' with strings containing spaces?
On Aug 16, 2011, at 4:53 PM, Mauricio Cornejo 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 functionalityRight... and what might that be... in a natural language if you please. Deducing intention from failed efforts in a computer language are a request for mind-reading. (Puts envelope to forehead. The answer is probably .... ?match ) switch() is designed to dispatch to a function call. match() is designed to return an item from a vector.> can be achieved by a different function(s), I'd most appreciate such > a suggestion as well. >> x <- c("Choice 1", "Choice 2", "Choice 3") > x <- c("Choice 1", "Choice 2", "Choice 3") > match("Choice 2", x) {1] 2 # returns a vector easily used as index to another vector > c("My first choice", "My 2nd choice","My 3rd choice") [ match("Choice 2", x)] [1] "My 2nd choice" David Winsemius, MD West Hartford, CT
Richard M. Heiberger
2011-Aug-16 22:59 UTC
[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 2ndchoice", "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 <mauriciocornejo@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > >[[alternative HTML version deleted]]
Apparently Analagous Threads
- Problems understanding use of regular expression (in gsub) for manipulating currency
- How to use access results of gregexpr in data frames
- How to subset based on column name that is a number ?
- How to limit string size when displaying data frames?
- Inexplicably different results using subset vs bracket notation on logical variable