SlideShare a Scribd company logo
Controlling the flow
Control flow statements
• Have you ever needed to quickly come up with numbers that add up to a
different number?
• Below is a code chunk that generates a sequence of numbers that add up to a
constant.
• It reads as follows: Give me a “y” amounts of numbers between “m” and “n” that
add up to “k”
• So this loop will repeat until it gets y different numbers between m and n that
add up to k.
• I controlled it by limiting the number of iterations to 80000 but you can change it
if you feel so inclined by changing the value of maxcount after you have copied
the code into your Rscript.
• k <- 170 (constant)
• m <- 5 (lower limit)
• n <- 20 (upper limit)
• x <- 1 (initialize the sequence)
• y <- 11 (size of data set)
• maxcount <- 80000 (maximum amount of iterations)
• counter <- 0
k <- 170 constant
m <- 5 lower limit
n <- 20 upper limit
x <- 1 initialize the sequence
y <- 11 size of data set
maxcount <- 80000 maximum amount of iterations
counter <- 0
while(sum(x) != k | counter < maxcount){
counter <- counter+1
if (sum(x)== k){
print(paste('Solution reached after', counter, 'observations'))
break
} else
x <- sample(m:n, y, replace = T)
if (counter> maxcount){
print(paste('Iterations exceed the maximum allowed number of', maxcount))
break }
}
[1] "Solution reached after 77 observations"
x
[1] 16 14 12 18 5 19 14 20 20 20 12
print('sum')
[1] "sum"
tibble(x)
A tibble: 11 x 1
x
<int>
1 16
2 14
3 12
4 18
5 5
6 19
7 14
8 20
9 20
10 20
11 12
print('count')
[1] "count"
counter
[1] 77
What if R cannot give you the required numbers? Break it up!
Here I chose some funky numbers on purpose to show how a break statement works:
• k <- 1000 (constant)
• m <- 0 (lower limit)
• n <- 0 (upper limit)
• x <- 1 (initialize the sequence)
• y <- 50 (size of data set)
• maxcount <- 80000 (maximum amount of iterations)
• counter <- 0
k <- 1000 constant
m <- 0 lower limit
n <- 0 upper limit
x <- 1 initialize the sequence
y <- 50 size of data set
maxcount <- 80000 maximum amount of iterations
counter <- 0
while(sum(x) != k | counter < maxcount){
counter <- counter+1
if (sum(x)== k){
print(paste('Solution reached after', counter, 'observations'))
break
} else
x <- sample(m:n, y, replace = T)
if (counter> maxcount){
print(paste('Iterations exceed the maximum allowed number of', maxcount))
break }
}
[1] "Iterations exceed the maximum allowed number of 80000"
print('sum')
[1] "sum"
tibble(x)
A tibble: 50 x 1
x
<int>
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
... with 40 more rows
print('count')
[1] "count"
counter
[1] 80001

More Related Content

DOC
algorithmdivs
PDF
The Ring programming language version 1.7 book - Part 28 of 196
PPTX
Chapter 7.4
PDF
The Ring programming language version 1.6 book - Part 44 of 189
PDF
The Ring programming language version 1.3 book - Part 32 of 88
PPTX
Statistics - ArgMax Equation
PDF
CLIM Undergraduate Workshop: Tutorial on R Software - Huang Huang, Oct 23, 2017
PPTX
Megadata With Python and Hadoop
algorithmdivs
The Ring programming language version 1.7 book - Part 28 of 196
Chapter 7.4
The Ring programming language version 1.6 book - Part 44 of 189
The Ring programming language version 1.3 book - Part 32 of 88
Statistics - ArgMax Equation
CLIM Undergraduate Workshop: Tutorial on R Software - Huang Huang, Oct 23, 2017
Megadata With Python and Hadoop

What's hot (20)

PDF
The Ring programming language version 1.10 book - Part 33 of 212
PDF
The Ring programming language version 1.4 book - Part 6 of 30
PPTX
mat lab introduction and basics to learn
PDF
PPT
C++ ammar .s.q
PPTX
laws of exponents
PDF
The Ring programming language version 1.5.4 book - Part 24 of 185
PDF
Pandas pythonfordatascience
PPTX
PDF
『あれ?Cssが反映されない!』を二度とやらないために
DOCX
Q1 create a java desktop application to find the largest number among the t...
PDF
Mission vim possible
PDF
Cheat Sheet for Machine Learning in Python: Scikit-learn
DOCX
CLUSTERGRAM
ODP
Parsec
PDF
Python matplotlib cheat_sheet
PDF
Python seaborn cheat_sheet
PPT
Analysis of Algorithms-Heapsort
PPTX
Devansh
The Ring programming language version 1.10 book - Part 33 of 212
The Ring programming language version 1.4 book - Part 6 of 30
mat lab introduction and basics to learn
C++ ammar .s.q
laws of exponents
The Ring programming language version 1.5.4 book - Part 24 of 185
Pandas pythonfordatascience
『あれ?Cssが反映されない!』を二度とやらないために
Q1 create a java desktop application to find the largest number among the t...
Mission vim possible
Cheat Sheet for Machine Learning in Python: Scikit-learn
CLUSTERGRAM
Parsec
Python matplotlib cheat_sheet
Python seaborn cheat_sheet
Analysis of Algorithms-Heapsort
Devansh
Ad

Similar to Understanding control-flow (20)

PPTX
Number system converted
PPTX
Introduction to matlab
PDF
PPTX
Introduction to MATLAB Programming for Engineers
PPTX
dld 01-introduction
PDF
Quick into to Software Transactional Memory in Frege
PPTX
Legacy lambda code
PPTX
Logic Circuits Design - "Chapter 1: Digital Systems and Information"
PPT
Introduction to Matlab - Basic Functions
PPSX
Row Pattern Matching in Oracle Database 12c
PDF
High-Performance Haskell
PPTX
Introduction to Matlab and application.pptx
PPTX
Writing Fast MATLAB Code
PDF
Using R Tool for Probability and Statistics
PPTX
Chapter 22. Lambda Expressions and LINQ
PPTX
2.3 SciPy library explained detialed 1.pptx
PPTX
R part I
PDF
Matlab-free course by Mohd Esa
PPTX
2.1 data represent on cpu
PPTX
matlab presentation fro engninering students
Number system converted
Introduction to matlab
Introduction to MATLAB Programming for Engineers
dld 01-introduction
Quick into to Software Transactional Memory in Frege
Legacy lambda code
Logic Circuits Design - "Chapter 1: Digital Systems and Information"
Introduction to Matlab - Basic Functions
Row Pattern Matching in Oracle Database 12c
High-Performance Haskell
Introduction to Matlab and application.pptx
Writing Fast MATLAB Code
Using R Tool for Probability and Statistics
Chapter 22. Lambda Expressions and LINQ
2.3 SciPy library explained detialed 1.pptx
R part I
Matlab-free course by Mohd Esa
2.1 data represent on cpu
matlab presentation fro engninering students
Ad

More from Casper Crause (9)

PPTX
Integrating R and Power BI
PDF
Handling missing data and outliers
PDF
Company segmentation - an approach with R
PDF
How to read multiple excel files - With R
PPTX
Storytelling By Visualization
PDF
Comparing Co2 Emissions Around The Globe
DOCX
Levelling up your chart skills
PDF
Wrangling data the tidy way with the tidyverse
PPTX
Project portfolio for Casper Crause
Integrating R and Power BI
Handling missing data and outliers
Company segmentation - an approach with R
How to read multiple excel files - With R
Storytelling By Visualization
Comparing Co2 Emissions Around The Globe
Levelling up your chart skills
Wrangling data the tidy way with the tidyverse
Project portfolio for Casper Crause

Recently uploaded (20)

PPTX
STERILIZATION AND DISINFECTION-1.ppthhhbx
PDF
OneRead_20250728_1808.pdfhdhddhshahwhwwjjaaja
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PPTX
sac 451hinhgsgshssjsjsjheegdggeegegdggddgeg.pptx
PPTX
Managing Community Partner Relationships
PDF
Transcultural that can help you someday.
PPTX
FMIS 108 and AISlaudon_mis17_ppt_ch11.pptx
PDF
REAL ILLUMINATI AGENT IN KAMPALA UGANDA CALL ON+256765750853/0705037305
PPTX
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
PPTX
DS-40-Pre-Engagement and Kickoff deck - v8.0.pptx
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked 2025}
PDF
Business Analytics and business intelligence.pdf
PPTX
IMPACT OF LANDSLIDE.....................
PDF
Tetra Pak Index 2023 - The future of health and nutrition - Full report.pdf
PDF
Optimise Shopper Experiences with a Strong Data Estate.pdf
DOCX
Factor Analysis Word Document Presentation
PDF
Microsoft 365 products and services descrption
PPTX
Topic 5 Presentation 5 Lesson 5 Corporate Fin
PPTX
New ISO 27001_2022 standard and the changes
PDF
Global Data and Analytics Market Outlook Report
STERILIZATION AND DISINFECTION-1.ppthhhbx
OneRead_20250728_1808.pdfhdhddhshahwhwwjjaaja
IBA_Chapter_11_Slides_Final_Accessible.pptx
sac 451hinhgsgshssjsjsjheegdggeegegdggddgeg.pptx
Managing Community Partner Relationships
Transcultural that can help you someday.
FMIS 108 and AISlaudon_mis17_ppt_ch11.pptx
REAL ILLUMINATI AGENT IN KAMPALA UGANDA CALL ON+256765750853/0705037305
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
DS-40-Pre-Engagement and Kickoff deck - v8.0.pptx
Capcut Pro Crack For PC Latest Version {Fully Unlocked 2025}
Business Analytics and business intelligence.pdf
IMPACT OF LANDSLIDE.....................
Tetra Pak Index 2023 - The future of health and nutrition - Full report.pdf
Optimise Shopper Experiences with a Strong Data Estate.pdf
Factor Analysis Word Document Presentation
Microsoft 365 products and services descrption
Topic 5 Presentation 5 Lesson 5 Corporate Fin
New ISO 27001_2022 standard and the changes
Global Data and Analytics Market Outlook Report

Understanding control-flow

  • 1. Controlling the flow Control flow statements • Have you ever needed to quickly come up with numbers that add up to a different number? • Below is a code chunk that generates a sequence of numbers that add up to a constant. • It reads as follows: Give me a “y” amounts of numbers between “m” and “n” that add up to “k” • So this loop will repeat until it gets y different numbers between m and n that add up to k. • I controlled it by limiting the number of iterations to 80000 but you can change it if you feel so inclined by changing the value of maxcount after you have copied the code into your Rscript. • k <- 170 (constant) • m <- 5 (lower limit) • n <- 20 (upper limit) • x <- 1 (initialize the sequence) • y <- 11 (size of data set) • maxcount <- 80000 (maximum amount of iterations) • counter <- 0 k <- 170 constant m <- 5 lower limit n <- 20 upper limit x <- 1 initialize the sequence y <- 11 size of data set maxcount <- 80000 maximum amount of iterations counter <- 0 while(sum(x) != k | counter < maxcount){ counter <- counter+1 if (sum(x)== k){ print(paste('Solution reached after', counter, 'observations')) break } else x <- sample(m:n, y, replace = T) if (counter> maxcount){
  • 2. print(paste('Iterations exceed the maximum allowed number of', maxcount)) break } } [1] "Solution reached after 77 observations" x [1] 16 14 12 18 5 19 14 20 20 20 12 print('sum') [1] "sum" tibble(x) A tibble: 11 x 1 x <int> 1 16 2 14 3 12 4 18 5 5 6 19 7 14 8 20 9 20 10 20 11 12 print('count') [1] "count" counter [1] 77 What if R cannot give you the required numbers? Break it up! Here I chose some funky numbers on purpose to show how a break statement works: • k <- 1000 (constant) • m <- 0 (lower limit) • n <- 0 (upper limit) • x <- 1 (initialize the sequence) • y <- 50 (size of data set) • maxcount <- 80000 (maximum amount of iterations) • counter <- 0
  • 3. k <- 1000 constant m <- 0 lower limit n <- 0 upper limit x <- 1 initialize the sequence y <- 50 size of data set maxcount <- 80000 maximum amount of iterations counter <- 0 while(sum(x) != k | counter < maxcount){ counter <- counter+1 if (sum(x)== k){ print(paste('Solution reached after', counter, 'observations')) break } else x <- sample(m:n, y, replace = T) if (counter> maxcount){ print(paste('Iterations exceed the maximum allowed number of', maxcount)) break } } [1] "Iterations exceed the maximum allowed number of 80000" print('sum') [1] "sum" tibble(x) A tibble: 50 x 1 x <int> 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 ... with 40 more rows print('count') [1] "count" counter [1] 80001