Control structures in R allow you to control the flow of execution of the program, depending on runtime conditions. Common structures are
-
@@ -66,11 +66,11 @@
diff --git a/02_RProgramming/ControlStructures/index.Rmd b/02_RProgramming/ControlStructures/index.Rmd
index 02571ce40..6214d166d 100644
--- a/02_RProgramming/ControlStructures/index.Rmd
+++ b/02_RProgramming/ControlStructures/index.Rmd
@@ -8,7 +8,7 @@ framework : io2012 # {io2012, html5slides, shower, dzslides, ...}
highlighter : highlight.js # {highlight.js, prettify, highlight}
hitheme : tomorrow #
url:
- lib: ../../libraries
+ lib: ../../librariesNew
assets: ../../assets
widgets : [mathjax] # {mathjax, quiz, bootstrap}
mode : selfcontained # {standalone, draft}
@@ -243,4 +243,4 @@ Summary
- Infinite loops should generally be avoided, even if they are theoretically correct.
-- Control structures mentiond here are primarily useful for writing programs; for command-line interactive work, the *apply functions are more useful.
\ No newline at end of file
+- Control structures mentiond here are primarily useful for writing programs; for command-line interactive work, the *apply functions are more useful.
diff --git a/02_RProgramming/ControlStructures/index.html b/02_RProgramming/ControlStructures/index.html
index 88194caca..3879ea60b 100644
--- a/02_RProgramming/ControlStructures/index.html
+++ b/02_RProgramming/ControlStructures/index.html
@@ -8,46 +8,46 @@
-
-
+
-
-
-
Roger Peng, Associate Professor
Johns Hopkins Bloomberg School of Public Health
Roger Peng, Associate Professor
Johns Hopkins Bloomberg School of Public Health
Control structures in R allow you to control the flow of execution of the program, depending on runtime conditions. Common structures are
if(<condition>) {
## do something
} else {
@@ -89,11 +89,11 @@ Control Structures: if
This is a valid if/else structure.
if(x > 3) {
@@ -116,11 +116,11 @@ if
Of course, the else clause is not necessary.
if(<condition1>) {
@@ -136,11 +136,11 @@ if
for
loops take an interator variable and assign it successive values from a sequence or vector. For loops are most commonly used for iterating over the elements of an object (list, vector, etc.)
for(i in 1:10) {
@@ -154,11 +154,11 @@ for
These three loops have the same behavior.
x <- c("a", "b", "c", "d")
@@ -182,11 +182,11 @@ for
for
loops can be nested.
x <- matrix(1:6, 2, 3)
@@ -204,11 +204,11 @@ Nested for loops
While loops begin by testing a condition. If it is true, then they execute the loop body. Once the loop body is executed, the condition is tested again, and so forth.
count <- 0
@@ -224,11 +224,11 @@ while
Sometimes there will be more than one condition in the test.
z <- 5
@@ -251,11 +251,11 @@ while
Repeat initiates an infinite loop; these are not commonly used in statistical applications but they do have their uses. The only way to exit a repeat
loop is to call break
.
x0 <- 1
@@ -276,22 +276,22 @@ repeat
The loop in the previous slide is a bit dangerous because there’s no guarantee it will stop. Better to set a hard limit on the number of iterations (e.g. using a for loop) and then report whether convergence was achieved or not.
next
is used to skip an iteration of a loop
for(i in 1:100) {
@@ -309,11 +309,11 @@ next, return
Summary