-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathdatatypes1.tex
493 lines (438 loc) · 10.9 KB
/
datatypes1.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
\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}{Objects}
R has five basic or ``atomic'' classes of objects:
\begin{itemize}
\item
character
\item
numeric (real numbers)
\item
integer
\item
complex
\item
logical (True/False)
\end{itemize}
The most basic object is a vector
\begin{itemize}
\item
A vector can only contain objects of the same class
\item
BUT: The one exception is a \textit{list}, which is represented as a
vector but can contain objects of different classes (indeed, that's
usually why we use them)
\end{itemize}
Empty vectors can be created with the \code{vector()} function.
\end{frame}
\begin{frame}{Numbers}
\begin{itemize}
\item
Numbers in R a generally treated as numeric objects (i.e. double
precision real numbers)
\item
If you explicitly want an integer, you need to specify the \code{L}
suffix
\item
Ex: Entering \code{1} gives you a numeric object; entering \code{1L}
explicitly gives you an integer.
\item
There is also a special number \code{Inf} which represents infinity;
e.g. \code{1 / 0}; \code{Inf} can be used in ordinary calculations;
e.g. \code{1 / Inf} is 0
\item
The value \code{NaN} represents an undefined value (``not a number'');
e.g. 0 / 0; \code{NaN} can also be thought of as a missing value (more
on that later)
\end{itemize}
\end{frame}
\begin{frame}{Attributes}
R objects can have attributes
\begin{itemize}
\item
names, dimnames
\item
dimensions (e.g. matrices, arrays)
\item
class
\item
length
\item
other user-defined attributes/metadata
\end{itemize}
Attributes of an object can be accessed using the \code{attributes()}
function.
\end{frame}
\begin{frame}[fragile]{Entering Input}
At the R prompt we type \textit{expressions}. The \code{<-} symbol is
the assignment operator.
\begin{verbatim}
> x <- 1
> print(x)
[1] 1
> x
[1] 1
> msg <- "hello"
\end{verbatim}
The grammar of the language determines whether an expression is
complete or not.
\begin{verbatim}
> x <- ## Incomplete expression
\end{verbatim}
The \code{\#} character indicates a \textit{comment}. Anything to the
right of the \code{\#} (including the \code{\#} itself) is ignored.
\end{frame}
\begin{frame}[fragile]{Evaluation}
When a complete expression is entered at the prompt, it is
\textit{evaluated} and the result of the evaluated expression is
returned. The result may be \textit{auto-printed}.
\begin{verbatim}
> x <- 5 ## nothing printed
> x ## auto-printing occurs
[1] 5
> print(x) ## explicit printing
[1] 5
\end{verbatim}
The \code{[1]} indicates that \code{x} is a vector and 5 is the first
element.
\end{frame}
\begin{frame}[fragile]{Printing}
\begin{verbatim}
> x <- 1:20
> x
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[16] 16 17 18 19 20
\end{verbatim}
The \code{:} operator is used to create integer sequences.
\end{frame}
\begin{frame}[fragile]{Creating Vectors}
The \code{c()} function can be used to create vectors of objects.
\begin{verbatim}
> x <- c(0.5, 0.6) ## numeric
> x <- c(TRUE, FALSE) ## logical
> x <- c(T, F) ## logical
> x <- c("a", "b", "c") ## character
> x <- 9:29 ## integer
> x <- c(1+0i, 2+4i) ## complex
\end{verbatim}
Using the \code{vector()} function
\begin{verbatim}
> x <- vector("numeric", length = 10)
> x
[1] 0 0 0 0 0 0 0 0 0 0
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Mixing Objects}
What about the following?
\begin{verbatim}
> y <- c(1.7, "a") ## character
> y <- c(TRUE, 2) ## numeric
> y <- c("a", TRUE) ## character
\end{verbatim}
When different objects are mixed in a vector, \textit{coercion} occurs
so that every element in the vector is of the same class.
\end{frame}
\begin{frame}[fragile]{Explicit Coercion}
Objects can be explicitly coerced from one class to another using the
\code{as.*} functions, if available.
\begin{verbatim}
> x <- 0:6
> class(x)
[1] "integer"
> as.numeric(x)
[1] 0 1 2 3 4 5 6
> as.logical(x)
[1] FALSE TRUE TRUE TRUE TRUE TRUE TRUE
> as.character(x)
[1] "0" "1" "2" "3" "4" "5" "6"
> as.complex(x)
[1] 0+0i 1+0i 2+0i 3+0i 4+0i 5+0i 6+0i
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Explicit Coercion}
Nonsensical coercion results in \code{NA}s.
\begin{verbatim}
> x <- c("a", "b", "c")
> as.numeric(x)
[1] NA NA NA
Warning message:
NAs introduced by coercion
> as.logical(x)
[1] NA NA NA
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Matrices}
Matrices are vectors with a \textit{dimension} attribute. The
dimension attribute is itself an integer vector of length 2 (nrow,
ncol)
\begin{verbatim}
> m <- matrix(nrow = 2, ncol = 3)
> m
[,1] [,2] [,3]
[1,] NA NA NA
[2,] NA NA NA
> dim(m)
[1] 2 3
> attributes(m)
$dim
[1] 2 3
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Matrices (cont'd)}
Matrices are constructed \textit{column-wise}, so entries can be
thought of starting in the ``upper left'' corner and running down the
columns.
\begin{verbatim}
> m <- matrix(1:6, nrow = 2, ncol = 3)
> m
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Matrices (cont'd)}
Matrices can also be created directly from vectors by adding a
dimension attribute.
\begin{verbatim}
> m <- 1:10
> m
[1] 1 2 3 4 5 6 7 8 9 10
> dim(m) <- c(2, 5)
> m
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{cbind-ing and rbind-ing}
Matrices can be created by \textit{column-binding} or
\textit{row-binding} with \code{cbind()} and \code{rbind()}.
\begin{verbatim}
> x <- 1:3
> y <- 10:12
> cbind(x, y)
x y
[1,] 1 10
[2,] 2 11
[3,] 3 12
> rbind(x, y)
[,1] [,2] [,3]
x 1 2 3
y 10 11 12
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Lists}
Lists are a special type of vector that can contain elements of
different classes. Lists are a very important data type in R and you
should get to know them well.
\begin{verbatim}
> x <- list(1, "a", TRUE, 1 + 4i)
> x
[[1]]
[1] 1
[[2]]
[1] "a"
[[3]]
[1] TRUE
[[4]]
[1] 1+4i
\end{verbatim}
\end{frame}
\begin{frame}{Factors}
Factors are used to represent categorical data. Factors can be
unordered or ordered. One can think of a factor as an integer vector
where each integer has a \textit{label}.
\begin{itemize}
\item
Factors are treated specially by modelling functions like \code{lm()}
and \code{glm()}
\item
Using factors with labels is \textit{better} than using integers
because factors are self-describing; having a variable that has values
``Male'' and ``Female'' is better than a variable that has values 1
and 2.
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Factors}
\begin{verbatim}
> x <- factor(c("yes", "yes", "no", "yes", "no"))
> x
[1] yes yes no yes no
Levels: no yes
> table(x)
x
no yes
2 3
> unclass(x)
[1] 2 2 1 2 1
attr(,"levels")
[1] "no" "yes"
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Factors}
The order of the levels can be set using the \code{levels} argument to
\code{factor()}. This can be important in linear modelling because
the first level is used as the baseline level.
\begin{verbatim}
> x <- factor(c("yes", "yes", "no", "yes", "no"),
levels = c("yes", "no"))
> x
[1] yes yes no yes no
Levels: yes no
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Missing Values}
Missing values are denoted by \code{NA} or \code{NaN} for undefined
mathematical operations.
\begin{itemize}
\item
\code{is.na()} is used to test objects if they are \code{NA}
\item
\code{is.nan()} is used to test for \code{NaN}
\item
\code{NA} values have a class also, so there are integer \code{NA},
character \code{NA}, etc.
\item
A \code{NaN} value is also \code{NA} but the converse is not true
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Missing Values}
\begin{verbatim}
> x <- c(1, 2, NA, 10, 3)
> is.na(x)
[1] FALSE FALSE TRUE FALSE FALSE
> is.nan(x)
[1] FALSE FALSE FALSE FALSE FALSE
> x <- c(1, 2, NaN, NA, 4)
> is.na(x)
[1] FALSE FALSE TRUE TRUE FALSE
> is.nan(x)
[1] FALSE FALSE TRUE FALSE FALSE
\end{verbatim}
\end{frame}
\begin{frame}{Data Frames}
Data frames are used to store tabular data
\begin{itemize}
\item
They are represented as a special type of list where every element of
the list has to have the same length
\item
Each element of the list can be thought of as a column and the length
of each element of the list is the number of rows
\item
Unlike matrices, data frames can store different classes of objects in
each column (just like lists); matrices must have every element be the
same class
\item
Data frames also have a special attribute called \code{row.names}
\item
Data frames are usually created by calling \code{read.table()} or
\code{read.csv()}
\item
Can be converted to a matrix by calling \code{data.matrix()}
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Data Frames}
\begin{verbatim}
> x <- data.frame(foo = 1:4, bar = c(T, T, F, F))
> x
foo bar
1 1 TRUE
2 2 TRUE
3 3 FALSE
4 4 FALSE
> nrow(x)
[1] 4
> ncol(x)
[1] 2
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Names}
R objects can also have names, which is very useful for writing
readable code and self-describing objects.
\begin{verbatim}
> x <- 1:3
> names(x)
NULL
> names(x) <- c("foo", "bar", "norf")
> x
foo bar norf
1 2 3
> names(x)
[1] "foo" "bar" "norf"
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Names}
Lists can also have names.
\begin{verbatim}
> x <- list(a = 1, b = 2, c = 3)
> x
$a
[1] 1
$b
[1] 2
$c
[1] 3
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Names}
And matrices.
\begin{verbatim}
> m <- matrix(1:4, nrow = 2, ncol = 2)
> dimnames(m) <- list(c("a", "b"), c("c", "d"))
> m
c d
a 1 3
b 2 4
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Summary}
Data Types
\begin{itemize}
\item atomic classes: numeric, logical, character, integer, complex
\item vectors, lists
\item factors
\item missing values
\item data frames
\item names
\end{itemize}
\end{frame}
\end{document}