Dear Bert, Thanks...I went through the doc pages but couldn't glean from that that for numeric options one has to backtick it. I is not given in the doc page...I actually had hundreds of different choices and so cannot depend on evaluation by position...Thanks again...I got to know that 2 is not a valid identifier but`2` is! Yours sincerely, AKSHAY M KULKARNI ________________________________ From: Bert Gunter <bgunter.4567 at gmail.com> Sent: Thursday, September 8, 2022 12:39 AM To: akshay kulkarni <akshay_e4 at hotmail.com> Cc: R help Mailing list <r-help at r-project.org> Subject: Re: [R] inconsistency in switch statements..... Well, as it states on the Help page, which should always be the first place to look for, ummm, help: "If the value of EXPR is not a character string it is coerced to integer. Note that this also happens for factors, with a warning, as typically the character level is meant. If the integer is between 1 and nargs()-1 then the corresponding element of ... is evaluated and the result returned: thus if the first argument is 3 then the fourth argument is evaluated and returned." So following up on Bill's comment, the arguments do not even need to be names when Stst evaluates to) an integer:> Stst <- 2 > switch(Stst, print("NO"), print("YES"))[1] "YES" Cheers, Bert On Wed, Sep 7, 2022 at 11:35 AM akshay kulkarni <akshay_e4 at hotmail.com> wrote:> > Dear members, > The following is my code: > > > Stst <- 2 > > switch(Stst, 1 = print("NO"), 2 = print("YES")) > Error: unexpected '=' in "switch(Stst, 1 =" > > Why isn't it printing "YES" on to the console? > > many thanks in advance. > > Thanking you, > Yours sincerely, > AKSHAY M KULKARNI > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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]]
Inline. On Wed, Sep 7, 2022 at 12:18 PM akshay kulkarni <akshay_e4 at hotmail.com> wrote:> > Dear Bert, > Thanks...I went through the doc pages but couldn't glean from that that for numeric options one has to backtick it.That's because your statement is false. Perhaps you misstated what you meant, but for numeric options, the match is **always** chosen by position, whether or not the arguments are labeled, whether or not the labels are backticked numerics or not.> a <- 2 > switch(a, `44` = 'No', howdy_pardner = 'Yes')[1] "Yes"> a = 44 ## can't work with only two positional choices > switch(a, `44` = 'No', howdy_pardner = 'Yes') > ## No match, so none returned and printed as above"I is not given in the doc page...I actually had hundreds of different choices and so cannot depend on evaluation by position..." .... which, to repeat, is false if choices evaluate to/can be coerced to integers. Evaluation is *always* by position in that case.> a <- 2.3 > switch(a, `44` = 'No', howdy_pardner = 'Yes')[1] "Yes" ## because> as.integer(2.3)[1] 2 -- Bert> > Yours sincerely, > AKSHAY M KULKARNI > > ________________________________ > From: Bert Gunter <bgunter.4567 at gmail.com> > Sent: Thursday, September 8, 2022 12:39 AM > To: akshay kulkarni <akshay_e4 at hotmail.com> > Cc: R help Mailing list <r-help at r-project.org> > Subject: Re: [R] inconsistency in switch statements..... > > Well, as it states on the Help page, which should always be the first > place to look for, ummm, help: > > "If the value of EXPR is not a character string it is coerced to > integer. Note that this also happens for factors, with a warning, as > typically the character level is meant. If the integer is between 1 > and nargs()-1 then the corresponding element of ... is evaluated and > the result returned: thus if the first argument is 3 then the fourth > argument is evaluated and returned." > > So following up on Bill's comment, the arguments do not even need to > be names when Stst evaluates to) an integer: > > > Stst <- 2 > > switch(Stst, print("NO"), print("YES")) > [1] "YES" > > Cheers, > Bert > > On Wed, Sep 7, 2022 at 11:35 AM akshay kulkarni <akshay_e4 at hotmail.com> wrote: > > > > Dear members, > > The following is my code: > > > > > Stst <- 2 > > > switch(Stst, 1 = print("NO"), 2 = print("YES")) > > Error: unexpected '=' in "switch(Stst, 1 =" > > > > Why isn't it printing "YES" on to the console? > > > > many thanks in advance. > > > > Thanking you, > > Yours sincerely, > > AKSHAY M KULKARNI > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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.
The argument names given to any function, including switch(), must be parsable as R 'names' (a.k.a. 'symbols'). Names are either a) sequences of letters, numbers, and underscores that begin with a letter or b) the contents of a backticked set of characters (with a few exceptions, like no \uxxxx sequences) Since calls to switch() are parsed with the same rules as any other function call, this is not mentioned in the help page for switch(). [Note that your original code died with a syntax error, not a run-time error that would be specific to switch().] -Bill On Wed, Sep 7, 2022 at 12:46 PM akshay kulkarni <akshay_e4 at hotmail.com> wrote:> Dear Bert, > Thanks...I went through the doc pages but couldn't > glean from that that for numeric options one has to backtick it. I is not > given in the doc page...I actually had hundreds of different choices and so > cannot depend on evaluation by position...Thanks again...I got to know that > 2 is not a valid identifier but`2` is! > > Yours sincerely, > AKSHAY M KULKARNI > > ________________________________ > From: Bert Gunter <bgunter.4567 at gmail.com> > Sent: Thursday, September 8, 2022 12:39 AM > To: akshay kulkarni <akshay_e4 at hotmail.com> > Cc: R help Mailing list <r-help at r-project.org> > Subject: Re: [R] inconsistency in switch statements..... > > Well, as it states on the Help page, which should always be the first > place to look for, ummm, help: > > "If the value of EXPR is not a character string it is coerced to > integer. Note that this also happens for factors, with a warning, as > typically the character level is meant. If the integer is between 1 > and nargs()-1 then the corresponding element of ... is evaluated and > the result returned: thus if the first argument is 3 then the fourth > argument is evaluated and returned." > > So following up on Bill's comment, the arguments do not even need to > be names when Stst evaluates to) an integer: > > > Stst <- 2 > > switch(Stst, print("NO"), print("YES")) > [1] "YES" > > Cheers, > Bert > > On Wed, Sep 7, 2022 at 11:35 AM akshay kulkarni <akshay_e4 at hotmail.com> > wrote: > > > > Dear members, > > The following is my code: > > > > > Stst <- 2 > > > switch(Stst, 1 = print("NO"), 2 = print("YES")) > > Error: unexpected '=' in "switch(Stst, 1 =" > > > > Why isn't it printing "YES" on to the console? > > > > many thanks in advance. > > > > Thanking you, > > Yours sincerely, > > AKSHAY M KULKARNI > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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]]
You DON'T need to use backticks. switch() is much older than backticks. Ordinary quotation marks are fine.> switch(as.character(1), "2"="YES", "1"="NO")[1] "NO" On Thu, 8 Sept 2022 at 07:46, akshay kulkarni <akshay_e4 at hotmail.com> wrote:> Dear Bert, > Thanks...I went through the doc pages but couldn't > glean from that that for numeric options one has to backtick it. I is not > given in the doc page...I actually had hundreds of different choices and so > cannot depend on evaluation by position...Thanks again...I got to know that > 2 is not a valid identifier but`2` is! > > Yours sincerely, > AKSHAY M KULKARNI > > ________________________________ > From: Bert Gunter <bgunter.4567 at gmail.com> > Sent: Thursday, September 8, 2022 12:39 AM > To: akshay kulkarni <akshay_e4 at hotmail.com> > Cc: R help Mailing list <r-help at r-project.org> > Subject: Re: [R] inconsistency in switch statements..... > > Well, as it states on the Help page, which should always be the first > place to look for, ummm, help: > > "If the value of EXPR is not a character string it is coerced to > integer. Note that this also happens for factors, with a warning, as > typically the character level is meant. If the integer is between 1 > and nargs()-1 then the corresponding element of ... is evaluated and > the result returned: thus if the first argument is 3 then the fourth > argument is evaluated and returned." > > So following up on Bill's comment, the arguments do not even need to > be names when Stst evaluates to) an integer: > > > Stst <- 2 > > switch(Stst, print("NO"), print("YES")) > [1] "YES" > > Cheers, > Bert > > On Wed, Sep 7, 2022 at 11:35 AM akshay kulkarni <akshay_e4 at hotmail.com> > wrote: > > > > Dear members, > > The following is my code: > > > > > Stst <- 2 > > > switch(Stst, 1 = print("NO"), 2 = print("YES")) > > Error: unexpected '=' in "switch(Stst, 1 =" > > > > Why isn't it printing "YES" on to the console? > > > > many thanks in advance. > > > > Thanking you, > > Yours sincerely, > > AKSHAY M KULKARNI > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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]]