1 Introduction to R
https://learn.datacamp.com/courses/free-introduction-to-r
1.1 Intro to basics
Arithmetic
Basic codes to perform calculation:## Name Code Output
## 1 An addition 5+5 10
## 2 A subtraction 5-5 0
## 3 A multiplication 3*5 15
## 4 A division (5+5)/2 5
## 5 Exonentiation 2^5 32
## 6 Modulo 28%%6 4
Variable Assignments
A variable
can store a value
(e.g. 4) or an object
(e.g. a function description) in R:
<- 2 my_socks
Basic Data Types
Here are some of the basic data types in R:
<- 5
my_numeric <- "abcdefg"
my_character <- FALSE my_logical
To check the data type of a variable, use the class()
function as follow:
class(my_numeric)
## [1] "numeric"
class(my_character)
## [1] "character"
class(my_logical)
## [1] "logical"
1.2 Vectors
Create A Vector
Create a vector with the function c()
:
<- c(140, -50, 20, -120, 240)
poker_vector <- c(-24, -50, 100, -350, 10) roulette_vector
Naming a Vector
To assign names to the elements of a vector, use the names()
function:
names(poker_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
names(roulette_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
Typing the days multiple times is too much work, so to save time, assign the days of the week vector to a variable: days_vector
, and then assign that to the other vectors with the class()
function like below:
<- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
days_vector names(poker_vector) <- c(days_vector)
names(roulette_vector) <- c(days_vector)
Calculating the winnings
This code below calculates the total profit from poker and roulette each day:
<- poker_vector + roulette_vector
total_daily total_daily
## Monday Tuesday Wednesday Thursday Friday
## 116 -100 120 -470 250
To calculate the sum of all elements of a vector, use the function sum()
.
<- sum(poker_vector)
total_poker <- sum(roulette_vector)
total_roulette <- total_poker + total_roulette
total_week total_week
## [1] -84
Vector Selection
To select elements of a vector, use square brackets []
:
<- poker_vector[3]
poker_wednesday poker_wednesday
## Wednesday
## 20
Two ways to select multiple elements from a vector:
<- poker_vector[c(2,3,4)]
poker_midweek <- poker_vector[2:4] poker_midweek
Another way to select is by using the assigned names of the vector elements above (Monday, Tuesday, …) instead of their numeric positions:
"Monday"] poker_vector[
## Monday
## 140
c("Monday","Tuesday")] poker_vector[
## Monday Tuesday
## 140 -50
Selection By Comparison
The (logical) comparison operators known to R are:
"<" for less than
">" for greater than
"<=" for less than or equal to
">=" for greater than or equal to
"==" for equal to each other
"!=" not equal to each other
Application of these comparison operators on vectors:
c(4, 5, 6) > 5
## [1] FALSE FALSE TRUE
To find the days that had a positive poker return:
> 0 poker_vector
## Monday Tuesday Wednesday Thursday Friday
## TRUE FALSE TRUE FALSE TRUE
To know not only the winning days, but also how much was made on those days:
<- poker_vector > 0
selection_vector <- poker_vector[selection_vector]
poker_winning_days poker_winning_days
## Monday Wednesday Friday
## 140 20 240
1.3 Matrices
A Matrix
To construct a matrix in R, use the matrix() function:
matrix(1:9, byrow = TRUE, nrow = 3)
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
The 1:9
is the data that’s written into the matrix. Here,1:9
is a shortcut for c(1, 2, 3, 4, 5, 6, 7, 8, 9)
. The part byrow
tells the matrix that the 1:9
is filled by the rows (left to right of each row from the top). To fill by the columns, write byrow = FALSE
. The third part nrow
tells the matrix to create 3 rows.
Example of a data-set used to create a matrix:
#Box office Star Wars (in millions)
<- c(460.998, 314.4)
new_hope <- c(290.475, 247.900)
empire_strikes <- c(309.306, 165.8)
return_jedi
#Create box_office (a vector)
<- c(new_hope, empire_strikes, return_jedi)
box_office
#Construct star_wars_matrix
<- matrix(box_office, byrow = TRUE, nrow = 3)
star_wars_matrix star_wars_matrix
## [,1] [,2]
## [1,] 460.998 314.4
## [2,] 290.475 247.9
## [3,] 309.306 165.8
Naming A Matrix
To add names ro the rows and columns of a matrix:
<- c("US", "non-US")
region <- c("A New Hope", "The Empire Strikes Back", "Return of the Jedi")
titles
colnames(star_wars_matrix) <- region
rownames(star_wars_matrix) <- titles
Calculating Rows’ or Columns’ Sum
Function rowSums()
calculates the sum of each row of a matrix. This would would give the total revenue of each movie from US + Non-US:
rowSums(star_wars_matrix)
## A New Hope The Empire Strikes Back Return of the Jedi
## 775.398 538.375 475.106
To calculate the total revenue of each region (US and Non-US) from all movies:
colSums(star_wars_matrix)
## US non-US
## 1060.779 728.100
Adding Row or Column To The Matrix
Add a column(s) to a matrix with the cbind()
function, which merge matrices and/or vectors together by column. For example:
<- rowSums(star_wars_matrix)
worldwide_vector <- cbind(star_wars_matrix, worldwide_vector)
all_wars_matrix all_wars_matrix
## US non-US worldwide_vector
## A New Hope 460.998 314.4 775.398
## The Empire Strikes Back 290.475 247.9 538.375
## Return of the Jedi 309.306 165.8 475.106
Selection of matrix elements
Use the square brackets [ ]
to select element(s) from a matrix. Vectors contain one type of data (like only x-axis) and matrices contain two types (like x and y-axis). So, use a comma to separate the rows, columns to be selected. For example:
my_matrix[1,2] selects the element at the first row and second column.
my_matrix[1:3,2:4] results in a matrix with the data on the rows 1, 2, 3 and columns 2, 3, 4.
To select all elements of a row or a column, no number is needed before or after the comma:
my_matrix[,1] selects all elements of the first column.
my_matrix[1,] selects all elements of the first row.
A little arithmetic with matrices
2 * matrix
would multiply each element of my_matrix by two.
matrix1 * matrix2
would create a new matrix where each element is the product of the corresponding elements in matrix1
and matrix2
.
-Estimated number of visitors
visitors <- all_wars_matrix/ticket_prices_matrix
1.4 Factors
There are two types of categorical variables: nominal categorical variable and ordinal categorical variable.
A nominal variable is a categorical variable without an implied order. For example, the categorical variable items_vector containing "Car", "Key", "Phone" and "Shoe"
. It’s hard to compare these elements using a standard.
Ordinal variables have natural ordering. For example, the categorical variable temperature_vector
with the categories: "Low", "Medium" and "High"
. The temps are comparable using a standard.
The function factor()
point out the different element groups in the data-set:
<- c("M", "F", "F", "M", "M")
survey_vector <- factor(survey_vector)
factor_survey_vector factor_survey_vector
## [1] M F F M M
## Levels: F M
Factor Levels(Naming Elements)
To change the factor levels “M” and “F” to “Male” and “Female”:
levels(factor_survey_vector) <- c("Female", "Male")
factor_survey_vector
## [1] Male Female Female Male Male
## Levels: Female Male
Summarizing a factor
To know how many "Male"
responses are in the study, and how many "Female"
responses. The summary()
function gives the answer to this question:
summary(factor_survey_vector)
## Female Male
## 2 3
Ordered factors (Comparing elements)
The function factor()
convert speed_vector
into unordered factor. To create an ordered factor, add two additional arguments: ordered
and levels
:
<- c("medium", "slow", "slow", "medium", "fast")
speed_vector <- factor(speed_vector, ordered = TRUE, levels = c("slow", "medium", "fast"))
factor_speed_vector factor_speed_vector
## [1] medium slow slow medium fast
## Levels: slow < medium < fast
The ordered = TRUE
is saying that the elements can be compared with a standard. The levels
state the elements of the factor in ranks.
Comparing ordered factors
The ordered factor_speed_vector
enables the comparison of different elements:
<- factor_speed_vector[2]
data_analyst2 <- factor_speed_vector[5]
data_analyst5
> data_analyst5 data_analyst2
## [1] FALSE
1.5 Data frames
Use function data.frame()
to create a data frame. Example from Datacamp below:
#Definition of vectors
<- c("Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune")
name <- c("Terrestrial planet", "Terrestrial planet", "Terrestrial planet", "Terrestrial planet", "Gas giant", "Gas giant", "Gas giant", "Gas giant")
type <- c(0.382, 0.949, 1, 0.532, 11.209, 9.449, 4.007, 3.883)
diameter <- c(58.64, -243.02, 1, 1.03, 0.41, 0.43, -0.72, 0.67)
rotation <- c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE)
rings
#Create a data frame from the vectors
<- data.frame(name, type, diameter, rotation, rings)
planets_df planets_df
## name type diameter rotation rings
## 1 Mercury Terrestrial planet 0.382 58.64 FALSE
## 2 Venus Terrestrial planet 0.949 -243.02 FALSE
## 3 Earth Terrestrial planet 1.000 1.00 FALSE
## 4 Mars Terrestrial planet 0.532 1.03 FALSE
## 5 Jupiter Gas giant 11.209 0.41 TRUE
## 6 Saturn Gas giant 9.449 0.43 TRUE
## 7 Uranus Gas giant 4.007 -0.72 TRUE
## 8 Neptune Gas giant 3.883 0.67 TRUE
Analyzation Tools
The function head()
and tail()
show show the first few and last few observations of a data frame.
The function str()
helps investigate the structure of the planets_df
data frame:
str(planets_df)
## 'data.frame': 8 obs. of 5 variables:
## $ name : chr "Mercury" "Venus" "Earth" "Mars" ...
## $ type : chr "Terrestrial planet" "Terrestrial planet" "Terrestrial planet" "Terrestrial planet" ...
## $ diameter: num 0.382 0.949 1 0.532 11.209 ...
## $ rotation: num 58.64 -243.02 1 1.03 0.41 ...
## $ rings : logi FALSE FALSE FALSE FALSE TRUE TRUE ...
Selection of data frame elements
Use []
to select specific elements from a data frame. Examples from Datacamp below:
#Print out data for Mars (entire fourth row)
4,] planets_df[
## name type diameter rotation rings
## 4 Mars Terrestrial planet 0.532 1.03 FALSE
#Select first 5 values of diameter column
1:5,"diameter"] planets_df[
## [1] 0.382 0.949 1.000 0.532 11.209
$
to select dataframe elements
The sign $
is a shortcut to select the wanted column that has a name:
#Select the rings variable from planets_df
<- planets_df$rings
rings_vector
rings_vector
## [1] FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE
To show the variable names of the data frame instead of only the result above:
#Select all columns for planets with rings
planets_df[rings_vector,]
## name type diameter rotation rings
## 5 Jupiter Gas giant 11.209 0.41 TRUE
## 6 Saturn Gas giant 9.449 0.43 TRUE
## 7 Uranus Gas giant 4.007 -0.72 TRUE
## 8 Neptune Gas giant 3.883 0.67 TRUE
subset()
to select dataframe elements
The subset(planets_df, subset = rings)
function give out the exact result like above.
Another example from Datacamp for variety:
#Select planets with diameter < 1
subset(planets_df, diameter < 1)
## name type diameter rotation rings
## 1 Mercury Terrestrial planet 0.382 58.64 FALSE
## 2 Venus Terrestrial planet 0.949 -243.02 FALSE
## 4 Mars Terrestrial planet 0.532 1.03 FALSE
Sorting
Use function sort()
to organize elements into ranks. To find out the order of planets according to diameter:
order(planets_df$diameter)
## [1] 1 4 2 3 8 7 6 5
The results indicate that planet #1 is the smallest, #4 is the second-smallest, and planet #5 is the largest. Now, to reorganize the data frame according to the ranking of diameter:
<- order(planets_df$diameter) positions
1.6 Lists
A list could contain matrices, vectors, other lists, … To create a list, use the function list()
:
my_list <- list(comp1, comp2 ...)
To give names to components of a list, there are two ways:
my_list <- list(your_comp1, your_comp2)
names(my_list) <- c("name1", "name2")
or
my_list <- list(name1 = your_comp1,
name2 = your_comp2)
To select elements from a list:
-Select the vector representing the actors
shining_list[["actors"]]
shining_list$actors
Select the second element of the vector representing the actors
shining_list[["actors"]][2]