-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathdatatypes2.tex
270 lines (228 loc) · 5.71 KB
/
datatypes2.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
261
262
263
264
265
266
267
268
269
270
\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{Data Types and Basic Operations}
%\author{Roger D. Peng}
% - Give the names in the same order as the appear in the paper.
% - Use the \inst{?} command only if the authors have different
% affiliation.
%\institute{
% \inst{1}%
% Department Biostatistics\\
% Johns Hopkins Bloomberg School of Public Health
% \and
% \inst{2}%
% Department of Preventive Medicine\\
% Feinberg School of Medicine, Northwestern University
%}
% - Use the \inst command only if there are several affiliations.
% - Keep it simple, no one is interested in your street address.
\date{Computing for Data Analysis}
\setbeamertemplate{footline}[page number]
\begin{document}
\begin{frame}
\titlepage
\end{frame}
\begin{frame}[fragile]{Subsetting}
There are a number of operators that can be used to extract subsets of
R objects.
\begin{itemize}
\item
\verb+[+ always returns an object of the same class as the original;
can be used to select more than one element (there is one exception)
\item
\verb+[[+ is used to extract elements of a list or a data frame; it
can only be used to extract a single element and the class of the
returned object will not necessarily be a list or data frame
\item
\verb+$+ is used to extract elements of a list or data frame by name;
semantics are similar to hat of \verb+[[+.
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Subsetting}
\begin{verbatim}
> x <- c("a", "b", "c", "c", "d", "a")
> x[1]
[1] "a"
> x[2]
[1] "b"
> x[1:4]
[1] "a" "b" "c" "c"
> x[x > "a"]
[1] "b" "c" "c" "d"
> u <- x > "a"
> u
[1] FALSE TRUE TRUE TRUE TRUE FALSE
> x[u]
[1] "b" "c" "c" "d"
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Subsetting a Matrix}
Matrices can be subsetted in the usual way with $(i, j)$ type indices.
\begin{verbatim}
> x <- matrix(1:6, 2, 3)
> x[1, 2]
[1] 3
> x[2, 1]
[1] 2
\end{verbatim}
Indices can also be missing.
\begin{verbatim}
> x[1, ]
[1] 1 3 5
> x[, 2]
[1] 3 4
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Subsetting a Matrix}
By default, when a single element of a matrix is retrieved, it is
returned as a vector of length 1 rather than a $1\times 1$ matrix.
This behavior can be turned off by setting \code{drop = FALSE}.
\begin{verbatim}
> x <- matrix(1:6, 2, 3)
> x[1, 2]
[1] 3
> x[1, 2, drop = FALSE]
[,1]
[1,] 3
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Subsetting a Matrix}
Similarly, subsetting a single column or a single row will give you a
vector, not a matrix (by default).
\begin{verbatim}
> x <- matrix(1:6, 2, 3)
> x[1, ]
[1] 1 3 5
> x[1, , drop = FALSE]
[,1] [,2] [,3]
[1,] 1 3 5
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Subsetting Lists}
\begin{verbatim}
> x <- list(foo = 1:4, bar = 0.6)
> x[1]
$foo
[1] 1 2 3 4
> x[[1]]
[1] 1 2 3 4
> x$bar
[1] 0.6
> x[["bar"]]
[1] 0.6
> x["bar"]
$bar
[1] 0.6
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Subsetting Lists}
Extracting multiple elements of a list.
\begin{verbatim}
> x <- list(foo = 1:4, bar = 0.6, baz = "hello")
> x[c(1, 3)]
$foo
[1] 1 2 3 4
$baz
[1] "hello"
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Subsetting Lists}
The \verb+[[+ operator can be used with \textit{computed} indices;
\verb+$+ can only be used with literal names.
\begin{verbatim}
> x <- list(foo = 1:4, bar = 0.6, baz = "hello")
> name <- "foo"
> x[[name]] ## computed index for `foo'
[1] 1 2 3 4
> x$name ## element `name' doesn't exist!
NULL
> x$foo
[1] 1 2 3 4 ## element `foo' does exist
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Subsetting Nested Elements of a List}
The \verb+[[+ can take an integer sequence.
\begin{verbatim}
> x <- list(a = list(10, 12, 14), b = c(3.14, 2.81))
> x[[c(1, 3)]]
[1] 14
> x[[1]][[3]]
[1] 14
> x[[c(2, 1)]]
[1] 3.14
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Partial Matching}
Partial matching of names is allowed with \verb+[[+ and \verb+$+.
\begin{verbatim}
> x <- list(aardvark = 1:5)
> x$a
[1] 1 2 3 4 5
> x[["a"]]
NULL
> x[["a", exact = FALSE]]
[1] 1 2 3 4 5
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Removing NA Values}
A common task is to remove missing values (\code{NA}s).
\begin{verbatim}
> x <- c(1, 2, NA, 4, NA, 5)
> bad <- is.na(x)
> x[!bad]
[1] 1 2 4 5
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Removing NA Values}
What if there are multiple things and you want to take the subset with
no missing values?
\begin{verbatim}
> x <- c(1, 2, NA, 4, NA, 5)
> y <- c("a", "b", NA, "d", NA, "f")
> good <- complete.cases(x, y)
> good
[1] TRUE TRUE FALSE TRUE FALSE TRUE
> x[good]
[1] 1 2 4 5
> y[good]
[1] "a" "b" "d" "f"
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Removing NA Values}
\begin{verbatim}
> airquality[1:6, ]
Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
5 NA NA 14.3 56 5 5
6 28 NA 14.9 66 5 6
> good <- complete.cases(airquality)
> airquality[good, ][1:6, ]
Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
7 23 299 8.6 65 5 7
8 19 99 13.8 59 5 8
\end{verbatim}
\end{frame}
\end{document}