CMT 429
Introduction to Data Science
Lecturer: David Gichohi
Lesson 5: R Control Structures and
Functions
CMT 409- Introdcution to Data Science 2
Content
• Conditional Statements
If
Else If
If Else
Nested If
AND/OR
• Loops
While
For
(Break, Next)
• Functions
CMT 409- Introdcution to Data Science 3
If statement
• Written with the if keyword, and it is used to specify a block of code
to be executed if a condition is TRUE.
a <- 33
b <- 200
if (b > a) {
print("b is greater than a")
}
CMT 409- Introdcution to Data Science 4
Else if statement
• "if the previous conditions were not true, then try this condition“
a <- 33
b <- 33
if (b > a) {
print("b is greater than a")
} else if (a == b) {
print ("a and b are equal")
}
CMT 409- Introdcution to Data Science 5
If Else Statement
• The else keyword catches anything which isn't caught by the
preceding conditions.
a <- 200
b <- 33
if (b > a) {
print("b is greater than a")
} else {
print("b is not greater than a")
}
CMT 409- Introdcution to Data Science 6
Nested If
• if statements inside if statements
x <- 41
if (x > 10) {
print("Above ten")
if (x > 20) {
print("and also above 20!")
} else {
print("but not above 20.")
}
} else {
print("below 10.")
}
CMT 409- Introdcution to Data Science 7
While Loop
• Example: Print i as long as i is less than 6:
i <- 1
while (i < 6) {
print(i)
i <- i + 1
}
• With the break statement, we can stop the loop even if the while condition is
TRUE:
i <- 1
while (i < 6) {
print(i)
i <- i + 1
if (i == 4) {
break
}
}
• With the next statement, we can skip an iteration without terminating the
loop
(NB: Replace ‘break’ with ‘next’ in above example.)
CMT 409- Introdcution to Data Science 8
For Loop
• A for loop is used for iterating over a sequence
for (x in 1:10) {
print(x)
}
• With the for loop we can execute a set of statements, once for each item in a vector, array, list, etc.
• Print every item in a list:
fruits <- list("apple", "banana", "cherry")
for (x in fruits) {
print(x)
}
• Print the number of dices:
dice <- c(1, 2, 3, 4, 5, 6)
for (x in dice) {
print(x)
}
CMT 409- Introdcution to Data Science 9
Nested Loop
• It is possible to place a loop inside another loop.
• Example: Print the adjective of each fruit in a list:
adj <- list("red", "big", "tasty")
fruits <- list("apple", "banana", "cherry")
for (x in adj) {
for (y in fruits) {
print(paste(x, y))
}
}
CMT 409- Introdcution to Data Science 10
Functions
• A function is a block of code which only runs when it is called.
• You can pass data, known as parameters, into a function.
• A function can return data as a result.
• To create a function, use the function() keyword.
• To call a function, use the function name followed by ().
# create a function with the name my_function
my_function <- function(){
print("Hello World!")
}
my_function() # call the function named my_function
CMT 409- Introdcution to Data Science 11
Function Arguments/Parameters
• Information can be passed into functions as arguments.
• Arguments are specified after the function name, inside the
parentheses.
• You can add as many arguments as you want, just separate them with
a comma.
• A parameter is the variable listed inside the parentheses in the
function definition while an argument is the value that is sent to the
function when it is called.
• To let a function return a result, use the return() function.
CMT 409- Introdcution to Data Science 12
Function Examples
1. my_function <- function(fname) {
paste(fname, "Griffin")
}
my_function("Peter")
my_function("Lois")
my_function("Stewie")
2. my_function <- function(fname, lname) {
paste(fname, lname)
}
my_function("Peter", "Griffin")
3. my_function <- function(country = "Norway") {
paste("I am from", country)
}
my_function("Sweden")
my_function("India")
my_function() # will get the default value, which is Norway
4. my_function <- function(x) {
return (5 * x)
}
print(my_function(3))
print(my_function(5))
print(my_function(9))
CMT 409- Introdcution to Data Science 13
Assignments
• Using AND/OR symbols, write a program that combine conditional
statements.
• Write a program demonstrate If .. Else Combined with a While Loop.
• Write a recursive function to compute factorial of a number.

More Related Content

DOCX
R Language
PPTX
欧洲杯投注-欧洲杯投注买球网站-欧洲杯投注买球网址|【​网址​🎉ac123.net🎉​】
PPTX
美洲杯下注-美洲杯下注最好的投注软件-美洲杯下注在哪个软件买球|【​网址​🎉ac22.net🎉​】
PPTX
欧洲杯足彩-欧洲杯足彩体彩-欧洲杯足彩竞彩|【​网址​🎉ac44.net🎉​】
PPTX
美洲杯投注-美洲杯投注竞猜-美洲杯投注竞猜投注|【​网址​🎉ac22.net🎉​】
PPTX
欧洲杯赌钱-欧洲杯赌钱比赛投注-欧洲杯赌钱比赛投注官网|【​网址​🎉ac22.net🎉​】
PPTX
世预赛投注-世预赛投注预测-世预赛投注押注|【​网址​🎉ac10.net🎉​】
PPTX
欧洲杯赌钱-欧洲杯赌钱冠军-欧洲杯赌钱冠军赔率|【​网址​🎉ac10.net🎉​】
R Language
欧洲杯投注-欧洲杯投注买球网站-欧洲杯投注买球网址|【​网址​🎉ac123.net🎉​】
美洲杯下注-美洲杯下注最好的投注软件-美洲杯下注在哪个软件买球|【​网址​🎉ac22.net🎉​】
欧洲杯足彩-欧洲杯足彩体彩-欧洲杯足彩竞彩|【​网址​🎉ac44.net🎉​】
美洲杯投注-美洲杯投注竞猜-美洲杯投注竞猜投注|【​网址​🎉ac22.net🎉​】
欧洲杯赌钱-欧洲杯赌钱比赛投注-欧洲杯赌钱比赛投注官网|【​网址​🎉ac22.net🎉​】
世预赛投注-世预赛投注预测-世预赛投注押注|【​网址​🎉ac10.net🎉​】
欧洲杯赌钱-欧洲杯赌钱冠军-欧洲杯赌钱冠军赔率|【​网址​🎉ac10.net🎉​】

Similar to Lesson 5 R Control Structures and Functions 2024 Notes.pptx (20)

PPTX
美洲杯下注-美洲杯下注八强-美洲杯下注十六强|【​网址​🎉ac123.net🎉​】
PPTX
data analysis using R programming language
PPTX
世预赛买球-世预赛买球下注-世预赛买球下注平台|【​网址​🎉ac123.net🎉​】
PPTX
欧洲杯投注-欧洲杯投注投注官方网站-欧洲杯投注买球投注官网|【​网址​🎉ac99.net🎉​】
PPTX
美洲杯买球-美洲杯买球下注平台-美洲杯买球投注平台|【​网址​🎉ac55.net🎉​】
PPTX
欧洲杯足彩-欧洲杯足彩八强-欧洲杯足彩十六强|【​网址​🎉ac99.net🎉​】
PPTX
美洲杯买球-美洲杯买球在哪里押注-美洲杯买球在哪里投注|【​网址​🎉ac44.net🎉​】
PPTX
欧洲杯投注-欧洲杯投注买球-欧洲杯投注买球网|【​网址​🎉ac22.net🎉​】
PPTX
美洲杯下注-美洲杯下注足彩竞猜-美洲杯下注竞猜app|【​网址​🎉ac99.net🎉​】
PPTX
美洲杯下注-美洲杯下注外围投注平台-美洲杯下注投注官方网站|【​网址​🎉ac99.net🎉​】
PPTX
欧洲杯足彩-欧洲杯足彩体育投注-欧洲杯足彩投注网站|【​网址​🎉ac99.net🎉​】
PPTX
世预赛在哪里押注-世预赛在哪里押注注册-世预赛在哪里押注|【​网址​🎉ac10.net🎉​】
PPTX
美洲杯押注靠谱的软件-美洲杯押注靠谱的软件推荐-美洲杯押注靠谱的软件|【​网址​🎉ac123.net🎉​】
PPTX
美洲杯体育投注-美洲杯体育投注推荐-美洲杯体育投注|【​网址​🎉ac10.net🎉​】
PPTX
Flow Controls in R Programming, gives a brief description of Flow controls in...
PPTX
世预赛押注app官网-世预赛押注app官网官方网站-世预赛押注app官网|【​网址​🎉ac99.net🎉​】
PPTX
2024欧洲杯比分投注-2024欧洲杯比分投注推荐-2024欧洲杯比分投注|【​网址​🎉ac99.net🎉​】
PPTX
世预赛买球网址-世预赛买球网址APP下载-世预赛买球网址|【​网址​🎉ac99.net🎉​】
PPTX
体育博彩论坛-十大体育博彩论坛-体育博彩论坛|【​网址​🎉ac55.net🎉​】
PPTX
美洲杯比赛投注官网-美洲杯比赛投注官网欢迎您-美洲杯比赛投注官网|【​网址​🎉ac10.net🎉​】
美洲杯下注-美洲杯下注八强-美洲杯下注十六强|【​网址​🎉ac123.net🎉​】
data analysis using R programming language
世预赛买球-世预赛买球下注-世预赛买球下注平台|【​网址​🎉ac123.net🎉​】
欧洲杯投注-欧洲杯投注投注官方网站-欧洲杯投注买球投注官网|【​网址​🎉ac99.net🎉​】
美洲杯买球-美洲杯买球下注平台-美洲杯买球投注平台|【​网址​🎉ac55.net🎉​】
欧洲杯足彩-欧洲杯足彩八强-欧洲杯足彩十六强|【​网址​🎉ac99.net🎉​】
美洲杯买球-美洲杯买球在哪里押注-美洲杯买球在哪里投注|【​网址​🎉ac44.net🎉​】
欧洲杯投注-欧洲杯投注买球-欧洲杯投注买球网|【​网址​🎉ac22.net🎉​】
美洲杯下注-美洲杯下注足彩竞猜-美洲杯下注竞猜app|【​网址​🎉ac99.net🎉​】
美洲杯下注-美洲杯下注外围投注平台-美洲杯下注投注官方网站|【​网址​🎉ac99.net🎉​】
欧洲杯足彩-欧洲杯足彩体育投注-欧洲杯足彩投注网站|【​网址​🎉ac99.net🎉​】
世预赛在哪里押注-世预赛在哪里押注注册-世预赛在哪里押注|【​网址​🎉ac10.net🎉​】
美洲杯押注靠谱的软件-美洲杯押注靠谱的软件推荐-美洲杯押注靠谱的软件|【​网址​🎉ac123.net🎉​】
美洲杯体育投注-美洲杯体育投注推荐-美洲杯体育投注|【​网址​🎉ac10.net🎉​】
Flow Controls in R Programming, gives a brief description of Flow controls in...
世预赛押注app官网-世预赛押注app官网官方网站-世预赛押注app官网|【​网址​🎉ac99.net🎉​】
2024欧洲杯比分投注-2024欧洲杯比分投注推荐-2024欧洲杯比分投注|【​网址​🎉ac99.net🎉​】
世预赛买球网址-世预赛买球网址APP下载-世预赛买球网址|【​网址​🎉ac99.net🎉​】
体育博彩论坛-十大体育博彩论坛-体育博彩论坛|【​网址​🎉ac55.net🎉​】
美洲杯比赛投注官网-美洲杯比赛投注官网欢迎您-美洲杯比赛投注官网|【​网址​🎉ac10.net🎉​】
Ad

More from 1045858 (6)

PPTX
Lesson 6- Data Visualization and Reporting.pptx
PPTX
Lesson 8- AI and Expert oiuyfSystems.pptx
PPTX
Lesson 7- Knowledge nnhgggManagement.pptx
PPTX
Lesson 5- Data Analysinbvs Techniques.pptx
PPTX
Lesson 9- Data Governance and Ethics.pptx
PPT
CS413_sunumu_04_Project_Management_Context_Presesntation.ppt
Lesson 6- Data Visualization and Reporting.pptx
Lesson 8- AI and Expert oiuyfSystems.pptx
Lesson 7- Knowledge nnhgggManagement.pptx
Lesson 5- Data Analysinbvs Techniques.pptx
Lesson 9- Data Governance and Ethics.pptx
CS413_sunumu_04_Project_Management_Context_Presesntation.ppt
Ad

Recently uploaded (20)

PDF
0520_Scheme_of_Work_(for_examination_from_2021).pdf
PPT
Acidosis in Dairy Herds: Causes, Signs, Management, Prevention and Treatment
PDF
Compact First Student's Book Cambridge Official
PDF
anganwadi services for the b.sc nursing and GNM
PDF
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
PPTX
UNIT_2-__LIPIDS[1].pptx.................
PDF
CAT 2024 VARC One - Shot Revision Marathon by Shabana.pptx.pdf
PDF
Lecture on Viruses: Structure, Classification, Replication, Effects on Cells,...
PPTX
2025 High Blood Pressure Guideline Slide Set.pptx
PDF
Laparoscopic Dissection Techniques at WLH
PDF
Fun with Grammar (Communicative Activities for the Azar Grammar Series)
PDF
fundamentals-of-heat-and-mass-transfer-6th-edition_incropera.pdf
PDF
faiz-khans about Radiotherapy Physics-02.pdf
DOCX
EDUCATIONAL ASSESSMENT ASSIGNMENT SEMESTER MAY 2025.docx
PPTX
Why I Am A Baptist, History of the Baptist, The Baptist Distinctives, 1st Bap...
PPTX
Power Point PR B.Inggris 12 Ed. 2019.pptx
PPTX
Neurological complocations of systemic disease
PPTX
Climate Change and Its Global Impact.pptx
PDF
Solved Past paper of Pediatric Health Nursing PHN BS Nursing 5th Semester
PDF
FYJC - Chemistry textbook - standard 11.
0520_Scheme_of_Work_(for_examination_from_2021).pdf
Acidosis in Dairy Herds: Causes, Signs, Management, Prevention and Treatment
Compact First Student's Book Cambridge Official
anganwadi services for the b.sc nursing and GNM
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
UNIT_2-__LIPIDS[1].pptx.................
CAT 2024 VARC One - Shot Revision Marathon by Shabana.pptx.pdf
Lecture on Viruses: Structure, Classification, Replication, Effects on Cells,...
2025 High Blood Pressure Guideline Slide Set.pptx
Laparoscopic Dissection Techniques at WLH
Fun with Grammar (Communicative Activities for the Azar Grammar Series)
fundamentals-of-heat-and-mass-transfer-6th-edition_incropera.pdf
faiz-khans about Radiotherapy Physics-02.pdf
EDUCATIONAL ASSESSMENT ASSIGNMENT SEMESTER MAY 2025.docx
Why I Am A Baptist, History of the Baptist, The Baptist Distinctives, 1st Bap...
Power Point PR B.Inggris 12 Ed. 2019.pptx
Neurological complocations of systemic disease
Climate Change and Its Global Impact.pptx
Solved Past paper of Pediatric Health Nursing PHN BS Nursing 5th Semester
FYJC - Chemistry textbook - standard 11.

Lesson 5 R Control Structures and Functions 2024 Notes.pptx

  • 1. CMT 429 Introduction to Data Science Lecturer: David Gichohi Lesson 5: R Control Structures and Functions
  • 2. CMT 409- Introdcution to Data Science 2 Content • Conditional Statements If Else If If Else Nested If AND/OR • Loops While For (Break, Next) • Functions
  • 3. CMT 409- Introdcution to Data Science 3 If statement • Written with the if keyword, and it is used to specify a block of code to be executed if a condition is TRUE. a <- 33 b <- 200 if (b > a) { print("b is greater than a") }
  • 4. CMT 409- Introdcution to Data Science 4 Else if statement • "if the previous conditions were not true, then try this condition“ a <- 33 b <- 33 if (b > a) { print("b is greater than a") } else if (a == b) { print ("a and b are equal") }
  • 5. CMT 409- Introdcution to Data Science 5 If Else Statement • The else keyword catches anything which isn't caught by the preceding conditions. a <- 200 b <- 33 if (b > a) { print("b is greater than a") } else { print("b is not greater than a") }
  • 6. CMT 409- Introdcution to Data Science 6 Nested If • if statements inside if statements x <- 41 if (x > 10) { print("Above ten") if (x > 20) { print("and also above 20!") } else { print("but not above 20.") } } else { print("below 10.") }
  • 7. CMT 409- Introdcution to Data Science 7 While Loop • Example: Print i as long as i is less than 6: i <- 1 while (i < 6) { print(i) i <- i + 1 } • With the break statement, we can stop the loop even if the while condition is TRUE: i <- 1 while (i < 6) { print(i) i <- i + 1 if (i == 4) { break } } • With the next statement, we can skip an iteration without terminating the loop (NB: Replace ‘break’ with ‘next’ in above example.)
  • 8. CMT 409- Introdcution to Data Science 8 For Loop • A for loop is used for iterating over a sequence for (x in 1:10) { print(x) } • With the for loop we can execute a set of statements, once for each item in a vector, array, list, etc. • Print every item in a list: fruits <- list("apple", "banana", "cherry") for (x in fruits) { print(x) } • Print the number of dices: dice <- c(1, 2, 3, 4, 5, 6) for (x in dice) { print(x) }
  • 9. CMT 409- Introdcution to Data Science 9 Nested Loop • It is possible to place a loop inside another loop. • Example: Print the adjective of each fruit in a list: adj <- list("red", "big", "tasty") fruits <- list("apple", "banana", "cherry") for (x in adj) { for (y in fruits) { print(paste(x, y)) } }
  • 10. CMT 409- Introdcution to Data Science 10 Functions • A function is a block of code which only runs when it is called. • You can pass data, known as parameters, into a function. • A function can return data as a result. • To create a function, use the function() keyword. • To call a function, use the function name followed by (). # create a function with the name my_function my_function <- function(){ print("Hello World!") } my_function() # call the function named my_function
  • 11. CMT 409- Introdcution to Data Science 11 Function Arguments/Parameters • Information can be passed into functions as arguments. • Arguments are specified after the function name, inside the parentheses. • You can add as many arguments as you want, just separate them with a comma. • A parameter is the variable listed inside the parentheses in the function definition while an argument is the value that is sent to the function when it is called. • To let a function return a result, use the return() function.
  • 12. CMT 409- Introdcution to Data Science 12 Function Examples 1. my_function <- function(fname) { paste(fname, "Griffin") } my_function("Peter") my_function("Lois") my_function("Stewie") 2. my_function <- function(fname, lname) { paste(fname, lname) } my_function("Peter", "Griffin") 3. my_function <- function(country = "Norway") { paste("I am from", country) } my_function("Sweden") my_function("India") my_function() # will get the default value, which is Norway 4. my_function <- function(x) { return (5 * x) } print(my_function(3)) print(my_function(5)) print(my_function(9))
  • 13. CMT 409- Introdcution to Data Science 13 Assignments • Using AND/OR symbols, write a program that combine conditional statements. • Write a program demonstrate If .. Else Combined with a While Loop. • Write a recursive function to compute factorial of a number.