If, else, and else ifIf StatementDecision making is an important part of programming. This can be achieved in R programming using the conditional if...else statement. We basically tell R that it should do something when test expression in () is true. Otherwise, R doesnt do anything.
The logical operators in R are as follows:
== means “equal”!= means “not equal”.< means “less than”.<= means “less than or equal”.> means “greater than”.>= means “greater than or equal”.if (test_expression) {
do something
}x <- 5
if(x > 0){
print("Positive number")
}## [1] "Positive number"
If and else StatementSometimes, we need to tell R do something else if the condition is false. For this purpose, we add else to our conditional statement.
if (see something) {
say something
} else {
dont say something
}x <- -5
if (x > 0){
print("Non-negative number")
} else {
print("Negative number")
}## [1] "Negative number"
If, else, and else if StatementSometimes we may want to have a couple of different conditions in the paranthesis and for each condition we execute different codes. The else if statement allows you to further customize your control structure. You can add as many else if statements as you like.
if (condition1) {
expr1
} else if (condition2) {
expr2
} else if (condition3) {
expr3
} else {
expr4
}x <- 0
if (x < 0) {
print("Negative number")
} else if (x > 0) {
print("Positive number")
} else {
print("Zero")
}## [1] "Zero"
Write a conditional statement with the following rules:
Hint: Use print() function.
major<-"PoliSci"major<-"PoliSci"
if (major=="PoliSci") {
print("The Major is Political Science")
} else if (major=="Econ") {
print("The Major is Economics")
} else if (major=="SocPol") {
print("The Major is Social Policy")
} else {
print("Unknown Major")
}## [1] "The Major is Political Science"
for (value in sequence) {
do something
}Loop version 1
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
for (liValue in linkedin) {
print(liValue)
}## [1] 16
## [1] 9
## [1] 13
## [1] 5
## [1] 2
## [1] 17
## [1] 14
Loop version 2
for (i in 1:length(linkedin)) {
print(linkedin[i])
}## [1] 16
## [1] 9
## [1] 13
## [1] 5
## [1] 2
## [1] 17
## [1] 14
The tic-tac-toe matrix has already been defined for you. Write a loop that tells me this:
"On row r and column c the board contains value".
Hint: Use nrow() and and ncol() to make your life easy.
Hint 2: You can use paste() function to combine numbers and characters.
ttt <- matrix(c("O", NA, "X", NA, "O", NA, "X", "O", "X"), nrow = 3, ncol = 3)
ttt## [,1] [,2] [,3]
## [1,] "O" NA "X"
## [2,] NA "O" "O"
## [3,] "X" NA "X"
ttt[1,3] shows me the value in the first row and the third column.
Hint: paste("Hi, my name is", i) produces “Hi, my name is Omer” for i=“Omer”
for (i in 1:nrow(ttt)) {
for (j in 1:ncol(ttt)) {
print(paste("On row", i, "and column", j, "the board contains", ttt[i,j]))
}
}## [1] "On row 1 and column 1 the board contains O"
## [1] "On row 1 and column 2 the board contains NA"
## [1] "On row 1 and column 3 the board contains X"
## [1] "On row 2 and column 1 the board contains NA"
## [1] "On row 2 and column 2 the board contains O"
## [1] "On row 2 and column 3 the board contains O"
## [1] "On row 3 and column 1 the board contains X"
## [1] "On row 3 and column 2 the board contains NA"
## [1] "On row 3 and column 3 the board contains X"
Now let’s say we have the following vector of students. Using the answer from Question 2, write a loop that will tell us this:
Aida studies Political Science
Bruce studies Unknown Major
and so on.
Hint: you can use paste() function.
Students<-c("Aida", "Bruce", "Cecilia", "Daniel")
Majors<-c("PoliSci", "Biology", "Econ", "SocPol")for(x in 1:length(Students)) {
if (Majors[x]=="PoliSci") {
print(paste(Students[x], "studies Political Science"))
} else if (Majors[x]=="Econ") {
print(paste(Students[x], "studies Economics"))
} else if (Majors[x]=="SocPol") {
print(paste(Students[x], "studies Social Policy"))
} else {
print(paste(Students[x], "studies Unknown Major"))
}
}## [1] "Aida studies Political Science"
## [1] "Bruce studies Unknown Major"
## [1] "Cecilia studies Economics"
## [1] "Daniel studies Social Policy"
x<-1
for(student in Students) {
if (Majors[x]=="PoliSci") {
print(paste(student, "studies Political Science"))
} else if (Majors[x]=="Econ") {
print(paste(student, "studies Economics"))
} else if (Majors[x]=="SocPol") {
print(paste(student, "studies Social Policy"))
} else {
print(paste(student, "studies Unknown Major"))
}
x<-x+1
}## [1] "Aida studies Political Science"
## [1] "Bruce studies Unknown Major"
## [1] "Cecilia studies Economics"
## [1] "Daniel studies Social Policy"
For repetitive tasks, we may feel too lazy to rewrite the code for each instance. Functions are great for this purpose. An R function is created by using the keyword function. The basic syntax of an R function definition is as follows:
function_name <- function(arg1, arg2){
Do something
return(something2)
}# Create a function power_two()
power_two <- function(x) {
return(x ^ 2)
}
# Use the function
power_two(-3)## [1] 9
# Create a function sum_abs()
sum_abs <- function(x, y) {
return(abs(x) + abs(y))
}
# Use the function
sum_abs(-12,-13)## [1] 25
Write an R function which converts Celsius into Fahrenheit.
Keep in mind that T(°F) = T(°C) × 1.8 + 32
Convert 20 C into F
Convert 0 C into F
myconverter<-function(C) {
return(C*1.8+32)
}
myconverter(20)## [1] 68
myconverter(0)## [1] 32
Let’s say we have the following vector of temperature over the past week:
TempinAD<-c(32,32,30,31.5,29,30,35)Write a loop which uses your function from Question 4.
for(i in TempinAD) {
print(myconverter(i))
}## [1] 89.6
## [1] 89.6
## [1] 86
## [1] 88.7
## [1] 84.2
## [1] 86
## [1] 95
Create a function where arguments are (1) temperature, (2) Measure (C/F). The function should transform C to F and F to C.
Write a loop which transforms TempinAD from C to F.
TempinAD<-c(32,32,30,31.5,29,30,35)autoconverter<-function(Temp,Type) {
if (Type=="C") {
return(Temp*1.8+32)
} else if (Type=="F") {
return((Temp-32)/1.8)
}
}
autoconverter(37.77778,"C")## [1] 100
autoconverter(100,"F")## [1] 37.77778
#Temp in Celcius
TempinAD## [1] 32.0 32.0 30.0 31.5 29.0 30.0 35.0
for(i in TempinAD){
print(autoconverter(i,"C"))
}## [1] 89.6
## [1] 89.6
## [1] 86
## [1] 88.7
## [1] 84.2
## [1] 86
## [1] 95
If(), else, and else if ()for() loopsfunction()return()print()paste()length()ncol()nrow()