🤠Conditional Statement
if statement
if statementAn "if statement" is written with the if keyword, and it is used to specify a block of code to be executed if a condition is TRUE:
if (condition){
# Statements
}# Age of two people
adam <- 33
bob <- 45
# Run the if statement
if (bob > adam){
print("bob is older than adam")
}
else if Statement
else if StatementThe else if keyword is R's way of saying "if the previous conditions were not true, then try this condition".
NOTE: We can add as many else if condition as we need.
if (condition){
# Statements
} else if (condition_2){
# Statements_2
} else if (condition_3){
# Statements_3
} ## Add as much "else if" as you need.
# Age of two people
adam <- 33
bob <- 33
# Run the if statement
if (bob > adam){
print("bob is older than adam")
} else if (bob == adam){
print("They are both same age")
}
else Statement
else StatementThe else keyword catches anything that isn't caught by the preceding conditions:
if (condition){
# Statements
} else if (condition_2){
# Statements_2
} else {
# Last Statement
}You can also use else without else if:
if (condition){
# Statements
} else {
# Last Statement
}a <- 200
b <- 33
if (b > a) {
print("b is greater than a")
} else if (a == b) {
print("a and b are equal")
} else {
print("a is greater than b")
}Code Breakdown:
In this example, a is greater than b, so the first condition is not true, also the else if condition is not true, so we go to the else condition and print to screen that "a is greater than b".

Nested if Statement
if StatementYou can also have if statements inside if statements, this is called nested if statements.
if (condition){
if (condition2){
# Statement
}
else {
# Statement2
}
}
else {
# Statement3
}x <- 40
if (x > 20) {
print("Larger than 20")
if (x %% 20 == 0) {
print("and also an even number")
} else {
print("but odd number")
}
} else {
print("below 20.")
}switch case Statement
switch case StatementSwitch case statements are a substitute for long if statements that compare a variable to several integral values. switch case in R is a multiway branch statement. It allows a variable to be tested for equality against a list of values.
The switch statement follows the approach of mapping and searching over a list of values. If there is more than one match for a specific value, then the switch statement will return the first match found of the value matched with the expression.
# Simple syntax
switch(expression, case1, case2, case3....)
# Syntax in Details
switch (expression,
"case 1" = Execute when the expression result matches case 1,
"case 2" = Execute when the expression result matches case 2,
"case 3" = When the expression result matches case 3, Execute these,
....
....
"case N" = When the expression result matches case N, Execute these lines,
Default Statements
)Here, the expression is matched with the list of values and the corresponding value is returned.
The expression value should be either integer or characters (We can write the expression as n/2 also, but the result should be an integer or convertible integer).
An R Switch statement allows us to add a default statement. The default statements will be executed if the Expression value or the Index_Position does not match any of the cases.
The first matching statement will be returned if there is more than one match.
weekDay <- 3
switch(weekDay,
'1' = print("Saturday"),
'2' = print("Sunday"),
'3' = print("Monday"),
'4' = print("Tuesday"),
'5' = print("Wednesday"),
'6' = print("Thursday"),
'7' = print("Friday"),
print("Not a valid input")
)Code Breakdown: Here,
weekDay: is the expression that takes the input.Our code will show output based on the input, when input matches with a case inside the code, it will print related output.
If the input doesn't match with any case inside the code, it will print the default case. In this code default case is
print("Not a valid input").
cal1 = 6
cal2 = 7
cal3 = "p"
switch(cal3,
"a"= cat("Addition =", cal1 + cal2),
"d"= cat("Subtraction =", cal1 - cal2),
"r"= cat("Division = ", cal1 / cal2),
"s"= cat("Multiplication =", cal1 * cal2),
"m"= cat("Modulus =", cal1 %% cal2),
"p"= cat("Power =", cal1 ^ cal2)
)Code Breakdown: Here,
The cat() function in R is used to concatenate objects and print them to the console or a file. The basic syntax of the function is cat(..., file = "", sep = " ", append = FALSE), where ... represents the objects to concatenate, file is the file name to send output to, sep is the separator to use between objects, and append is a logical value indicating whether to append output to an existing file or create a new file. The function can be used in different ways, such as concatenating strings with a specified separator, outputting results to a file, and printing character strings in a readable format. To save the output of the cat() function as a value in R, it is recommended to use the paste() function instead.

Sources
The sources of the contents on this page are:
Last updated