Lab 3 (Tutorial 1)
Lab 3 (Tutorial 1)
1/20
Today: more basic commands in R
2/20
Recall: create vectors
## [1] 74 75 66
3/20
Combine vectors into a data frame
##
CountryName LifeExpectancy
1 Brazil 74
2 China 75
3 India 66
4/20
Add a vector to a data frame
We use $:
name of the data frame $ name of the new variable <- vector of values
5/20
Some logical statements in R
6/20
Some logical statements in R
• |: OR
## [1] TRUE
• &: AND
1==0 & "HKU" != "Hku"
## [1] FALSE
7/20
The class function outputs the class of the object
x = 5
class(x)
## [1] "numeric"
## [1] "character"
z = TRUE
class(z)
## [1] "logical"
8/20
Subset the data
Example: Subset the data frame WHO to only contain countries in the
Europe region, the % of population under 15 ă 14, and fertility rate ă 1.5
9/20
Identify the extreme value
10/20
Basic plots in R
11/20
Basic plots in R
12/20
Basic plots in R
13/20
Summary tables in R
14/20
Summary tables in R
15/20
Summary tables in R
16/20
Summary tables in R
17/20
Summary tables in R
18/20
Here are the commands/operators we covered today:
• data.frame()
• $
• ==, !=, >, <, >=, <=, |, &
• class()
• subset()
• which.min(), which.max()
• plot(), hist(), boxplot()
• table(), tapply()
19/20
Acknowledgement
20/20