-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathcontrolstructures.tex
262 lines (211 loc) · 5.49 KB
/
controlstructures.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
\documentclass[aspectratio=169]{beamer}
\mode<presentation>
{
\usetheme{Warsaw}
% or ...
\setbeamercovered{transparent}
% or whatever (possibly just delete it)
}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage{graphicx}
%\usepackage{times}
%\usepackage[T1]{fontenc}
% Or whatever. Note that the encoding and the font should match. If T1
% does not look nice, try deleting the line with the fontenc.
\usepackage{amsmath,amsfonts,amssymb}
\input{macros}
\title[The R Language]{Introduction to the R Language}
\subtitle{Control Structures}
\date{Computing for Data Analysis}
\setbeamertemplate{footline}[page number]
\begin{document}
\begin{frame}
\titlepage
\end{frame}
\begin{frame}{Control Structures}
Control structures in R allow you to control the flow of execution of
the program, depending on runtime conditions. Common structures are
\begin{itemize}
\item
\code{if}, \code{else}: testing a condition
\item
\code{for}: execute a loop a fixed number of times
\item
\code{while}: execute a loop \textit{while} a condition is true
\item
\code{repeat}: execute an infinite loop
\item
\code{break}: break the execution of a loop
\item
\code{next}: skip an interation of a loop
\item
\code{return}: exit a function
\end{itemize}
Most control structures are not used in interactive sessions, but
rather when writing functions or longer expresisons.
\end{frame}
\begin{frame}[fragile]{Control Structures: if}
\begin{verbatim}
if(<condition>) {
## do something
} else {
## do something else
}
if(<condition1>) {
## do something
} else if(<condition2>) {
## do something different
} else {
## do something different
}
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{if}
This is a valid if/else structure.
\begin{verbatim}
if(x > 3) {
y <- 10
} else {
y <- 0
}
\end{verbatim}
So is this one.
\begin{verbatim}
y <- if(x > 3) {
10
} else {
0
}
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{if}
Of course, the \code{else} clause is not necessary.
\begin{verbatim}
if(<condition1>) {
}
if(<condition2>) {
}
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{for}
\code{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.)
\begin{verbatim}
for(i in 1:10) {
print(i)
}
\end{verbatim}
This loop takes the \code{i} variable and in each iteration of the
loop gives it values 1, 2, 3, ..., 10, and then exits.
\end{frame}
\begin{frame}[fragile]{for}
These three loops have the same behavior.
\begin{verbatim}
x <- c("a", "b", "c", "d")
for(i in 1:4) {
print(x[i])
}
for(i in seq_along(x)) {
print(x[i])
}
for(letter in x) {
print(letter)
}
for(i in 1:4) print(x[i])
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Nested for loops}
\code{for} loops can be nested.
\begin{verbatim}
x <- matrix(1:6, 2, 3)
for(i in seq_len(nrow(x))) {
for(j in seq_len(ncol(x))) {
print(x[i, j])
}
}
\end{verbatim}
Be careful with nesting though. Nesting beyond 2--3 levels is often
very difficult to read/understand.
\end{frame}
\begin{frame}[fragile]{while}
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.
\begin{verbatim}
count <- 0
while(count < 10) {
print(count)
count <- count + 1
}
\end{verbatim}
While loops can potentially result in infinite loops if not written
properly. Use with care!
\end{frame}
\begin{frame}[fragile]{while}
Sometimes there will be more than one condition in the test.
\begin{verbatim}
z <- 5
while(z >= 3 && z <= 10) {
print(z)
coin <- rbinom(1, 1, 0.5)
if(coin == 1) { ## random walk
z <- z + 1
} else {
z <- z - 1
}
}
\end{verbatim}
Conditions are always evaluated from left to right.
\end{frame}
\begin{frame}[fragile]{repeat}
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 \code{repeat} loop is to call \code{break}.
\begin{verbatim}
x0 <- 1
tol <- 1e-8
repeat {
x1 <- computeEstimate()
if(abs(x1 - x0) < tol) {
break
} else {
x0 <- x1
}
}
\end{verbatim}
\end{frame}
\begin{frame}{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.
\end{frame}
\begin{frame}[fragile]{next, return}
\code{next} is used to skip an iteration of a loop
\begin{verbatim}
for(i in 1:100) {
if(i <= 20) {
## Skip the first 20 iterations
next
}
## Do something here
}
\end{verbatim}
\code{return} signals that a function should exit and return a given
value
\end{frame}
\begin{frame}{Control Structures}
Summary
\begin{itemize}
\item Control structures like \code{if}, \code{while}, and \code{for}
allow you to control the flow of an R program
\item Infinite loops should generally be avoided, even if they are
theoretically correct.
\item Control structures mentiond here are primarily useful for
writing programs; for command-line interactive work, the *apply
functions are more useful.
\end{itemize}
\end{frame}
\end{document}