Vuong Ba Thinh
HCMC University of Technology
vbthinh@hcmut.edu.vn
R Tutorial
1 / 24
Overview
1
Introduction
What is R!
Downloading and Installing R
RStudio
R Tutorial
2 / 24
What is R!
R Tutorial
3 / 24
Free software
R Tutorial
4 / 24
R website: http://www.r-project.org/
R Tutorial
5 / 24
RStudio
R Tutorial
6 / 24
R Tutorial
7 / 24
Numbers
R Tutorial
8 / 24
Attributes
R Tutorial
9 / 24
Entering Input
At the R prompt we type expressions. The <- symbol is the
assignment operator
>x <- 1
>print(x)
[1] 1
>x
[1] 1
>msg <- hello
R Tutorial
10 / 24
Evaluation
R Tutorial
11 / 24
Printing
R Tutorial
12 / 24
Creating Vectors
The c() function can be used to create vectors of objects.
>x
>x
>x
>x
>x
>x
<<<<<<-
R Tutorial
13 / 24
Matrices
Matrices are vectors with a dimension attribute. The dimension
attribute is itself an integer vector of length 2 (nrow, ncol).
>m <- matrix(nrow = 2, ncol = 3)
>m
???
>dim(m)
[1]2 3
>attributes(m)
$dim
[1]2 3
Try
>m <- matrix(1:6, nrow = 2, ncol = 3)
R Tutorial
14 / 24
Lists
Lists are a special type of vector that can contain elements of diferent
classes. Lists are a very important data type in R and you should get
to know them well.
>x <- list(1, a, TRUE, 1 + 4i)
>x
???
R Tutorial
15 / 24
Missing Values
NA values have a class also, so there are integer NA, character NA,
etc.
NaN value is also NA but the converse is not true.
with x = c(1, 2, NA, 4, 5) and y = c(1, 2, NaN, 4, 5), try is.na(x),
is.nan(x), is.na(y), and y.nan(y).
R Tutorial
16 / 24
Data Frames
R Tutorial
17 / 24
Subsetting
R Tutorial
18 / 24
Subsetting (2)
R Tutorial
19 / 24
Subsetting a matrix
R Tutorial
20 / 24
Subsetting a list
>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
R Tutorial
21 / 24
Vectorized Operations
R Tutorial
22 / 24
To be continued in Assignment
R Tutorial
23 / 24
The End
R Tutorial
24 / 24