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]]
1 and 2 are not valid identifiers in R, so you need to surround them with backticks to make them valid, the same as quoting a string: switch(Stst, `1` = print("NO"), `2` = print("YES")) On Wed., Sep. 7, 2022, 14:35 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]]
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.
(Typos in my last paragraph corrected for clarity) On Wed, Sep 7, 2022 at 12:09 PM Bert Gunter <bgunter.4567 at gmail.com> wrote:> > 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 named 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.