[R] inconsistency in switch statements.....

akshay kulkarni @k@h@y_e4 @end|ng |rom hotm@||@com
Wed Sep 7 21:10:33 CEST 2022


Dear Bill,
                  Thanks for the accurate reply....

yours sincerely,
AKSHAY M KULKARNI

________________________________
From: Bill Dunlap <williamwdunlap using gmail.com>
Sent: Thursday, September 8, 2022 12:28 AM
To: akshay kulkarni <akshay_e4 using hotmail.com>
Cc: Andrew Simmons <akwsimmo using gmail.com>; R help Mailing list <r-help using r-project.org>
Subject: Re: [R] inconsistency in switch statements.....

It doesn't really work.  If the first argument to switch() is a character string then it chooses the argument by name; for a number it chooses by position.  In your case the name and position matched so you did not notice the difference.

> switch(2, `2` = "first", `1` = "second")
[1] "second"
> switch("2", `2` = "first", `1` = "second")
[1] "first"

-Bill



On Wed, Sep 7, 2022 at 11:46 AM akshay kulkarni <akshay_e4 using hotmail.com<mailto:akshay_e4 using hotmail.com>> wrote:
Dear Andrew,
                         Its working. Thanks a lot....

Yours sincerely,
AKSHAY M KULKARNI

________________________________
From: Andrew Simmons <akwsimmo using gmail.com<mailto:akwsimmo using gmail.com>>
Sent: Thursday, September 8, 2022 12:08 AM
To: akshay kulkarni <akshay_e4 using hotmail.com<mailto:akshay_e4 using hotmail.com>>
Cc: R help Mailing list <r-help using r-project.org<mailto:r-help using r-project.org>>
Subject: Re: [R] inconsistency in switch statements.....

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 using hotmail.com<mailto:akshay_e4 using hotmail.com><mailto:akshay_e4 using hotmail.com<mailto:akshay_e4 using 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 using r-project.org<mailto:R-help using r-project.org><mailto:R-help using r-project.org<mailto:R-help using 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 using r-project.org<mailto:R-help using 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]]



More information about the R-help mailing list