How to use the switch statement in R functions

0 votes
I'd like to utilise the switch() statement in R for my function to trigger different computations depending on the value of the function's input.

In Matlab, for example, you may achieve this by writing

switch(AA)

1st case

...

2nd case

...

'3' case

...

end

I found this post - switch() statement usage - that explains how to use switch, but it's not really helpful for me because I want to do more complex calculations (matrix operations) rather than a basic mean.
Jun 14, 2022 in Data Analytics by Avinash
• 1,260 points
546 views

1 answer to this question.

0 votes
Switch definitely wasn't intended to operate this way, but you can:

"foo" switch = AA

(AA, foo=# case 'foo' here... print('foo'), bar=# case 'bar' here... print('bar'), print('default') )...each case is an expression - normally just a simple thing, but here I use a curly-block so you can jam any code you want in there.
answered Jun 24, 2022 by Sohail
• 3,040 points

Related Questions In Data Analytics

0 votes
0 answers

How to use the where clause in R programming?

I'm trying to implement a where clause ...READ MORE

Dec 24, 2018 in Data Analytics by Sophie may
• 10,620 points
4,170 views
0 votes
1 answer

how to use the Box-Cox power transformation in R

Yes, you are on the right track ...READ MORE

answered Jun 22, 2023 in Data Analytics by anonymous
• 1,380 points
1,061 views
0 votes
2 answers

How to use group by for multiple columns in dplyr, using string vector input in R?

data = data.frame(   zzz11def = sample(LETTERS[1:3], 100, replace=TRUE),   zbc123qws1 ...READ MORE

answered Aug 6, 2019 in Data Analytics by anonymous
14,005 views
0 votes
1 answer

How to find out the sum/mean for multiple variables per group in R?

You can use the reshape2 package for ...READ MORE

answered Apr 12, 2018 in Data Analytics by DataKing99
• 8,250 points
3,676 views
0 votes
1 answer

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,610 points
974 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
1,061 views
0 votes
1 answer

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
5,812 views
0 votes
1 answer

Left Join and Right Join using "dplyr"

The below is the code to perform ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,660 points
993 views
0 votes
1 answer

How to use Aggregate function in R

Where dat is the name of your ...READ MORE

answered Jun 24, 2022 in Data Analytics by Sohail
• 3,040 points
552 views
0 votes
1 answer

How to use the strsplit function with a period

Since the dot has a specific meaning ...READ MORE

answered Jun 24, 2022 in Data Analytics by Sohail
• 3,040 points
721 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP