if - else if - else statement and brackets

0 votes
According to what I've learned, the standard approach to write a "if - else if" sentence is as follows:

If (2==1), print("1"), else if (2==2), print("2"), otherwise print("3")
or

if (2==1) print("1") else print("2") if (2==2) print("2") else print ("3")
Quite the opposite, If I continue to write in this manner,

if (2==1) print("1") otherwise (print("3") or this way: if (2==2) print("2") else (print("3")

The statement does not work if (2==1) print("1") otherwise if (2==2) print("2") else print("3") Can you explain why else or else must come before else or else if they're on the same line? Is there any way to write the if-else if-else sentence in R without using brackets?
Jun 14, 2022 in Data Analytics by Avinash
• 1,260 points
1,047 views

1 answer to this question.

0 votes
When the first if is followed by a compound expression (marked by the pair), the parser will assume that the expression after else is compound as well. The only time else is explicitly used is in compound expressions. This is mentioned explicitly in the documentation: if(cond) cons.expr else alt.expr, where cons.expr and alt.expr are both compound. You can work around this by using the way R parses function definitions, as @Berry mentioned, but it's better to be consistent with bracket usage (IMO). – hrbrmstr hrbrmstr hrbrmstr
 

It will also work if you surround the uncommon if-else in brackets (bad if-else expr) or in a function (function()) that is more common (bad if-else expr) –

rawr
answered Jun 14, 2022 by Sohail
• 3,040 points

Related Questions In Data Analytics

0 votes
1 answer

How to check if a directory exists and how to create and create if doesn't exist?

You can use showWarnings = FALSE NOTE:  showWarnings ...READ MORE

answered Apr 17, 2018 in Data Analytics by DataKing99
• 8,250 points
2,614 views
+1 vote
2 answers

How to replace a value in a data frame based on a conditional 'If' statement?

It's easier to convert alpha to characters ...READ MORE

answered Jun 6, 2018 in Data Analytics by Sahiti
• 6,370 points
36,680 views
0 votes
1 answer

If-else in R code

x <- c(.5, 0.2, 2, 3.4, 0.4, ...READ MORE

answered Nov 5, 2018 in Data Analytics by Kalgi
• 52,350 points
764 views
0 votes
1 answer

How to edit the labels and limit if a plot using ggplot? - R

Add a limit to axis ticks using ...READ MORE

answered Nov 3, 2019 in Data Analytics by anonymous
• 33,030 points
726 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,570 points
1,048 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,226 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,912 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
1,158 views
0 votes
1 answer

Vectorized IF statement in R?

x <- seq(0.1,10,0.1) > x [1] ...READ MORE

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

Use of $ and %% operators in R

According to help('percent in percent'), percent in ...READ MORE

answered Jun 23, 2022 in Data Analytics by Sohail
• 3,040 points
774 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