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 in Data Analytics by Avinash
• 1,260 points
17 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 by Sohail
• 2,960 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,590 points
3,059 views
0 votes
0 answers

how to use the Box-Cox power transformation in R

I've read that Box-Cox can help determine ...READ MORE

Jul 6 in Data Analytics by avinash
• 1,840 points
9 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
11,743 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,240 points
2,611 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
434 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
478 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
4,345 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
502 views
0 votes
1 answer

How to use Aggregate function in R

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

answered Jun 24 in Data Analytics by Sohail
• 2,960 points
18 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 in Data Analytics by Sohail
• 2,960 points
17 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP