Chapter 1 R 101
1.1 Working Directory
getwd()
allows us to get our current Working directory.
setwd()
allows us to set out working directory to which ever location we want it to be. Preferably to set this to the same directory as the project folder is.
getwd()
setwd("C:/Users/Logan/Documents")
1.2 Basic Arithmetic
At the heart of R programming, its a big calculator.
Arithmetic Operators like +, -, *, /.
Addition
4 + 2
## [1] 6
Subtraction
4 - 2
## [1] 2
Multiplication
4 * 2
## [1] 8
Division
9 / 2
## [1] 4.5
Combination
(4 * 3) / (2 + 4)
## [1] 2
3 + 4 * 3
## [1] 15
Remainder
14%%5
## [1] 4
Quotient
14%/%5
## [1] 2
Other generic mathematical function like log and square root are also available.
Power
2^69
## [1] 5.902958e+20
Log
log(13)
## [1] 2.564949
Square Root
sqrt(144)
## [1] 12
1.3 Defining Variables & Objects
Assignment operator <-
is used to assigned the value to a variable It sets an object(everything) to a certain variable.
new_variable <- "Hello World!"
new_variable
## [1] "Hello World!"
x <- 5 + 6
x
## [1] 11
Importance of spacing
y<-3
y< -3
## [1] FALSE
1.4 Vector
Vectors are created using c()
function. In R, vector are just an array of number. Also known as row matrices in mathematics.
Create a vector.
apple <- c('red','green',"yellow")
apple
## [1] "red" "green" "yellow"
1.5 Types of object
Like many other programming language, R have various types of object as well.
- numeric
- complex
- integer
- logical
- character
We can check its type using the class() function
Numeric
# creates a vector of double (decimal) values
a <- c(8.9, 1.0, 1.4)
class(a)
## [1] "numeric"
Complex
# creates a vector of imaginary values
b <- c(1 + 2i, 4 - 8i)
class(b)
## [1] "complex"
Integer
# creates a vector of integer (whole numbers) values
p <- c(23L, 44L)
class(p)
## [1] "integer"
Logical
# creates a vector of true or false
q <- 4 == 5
class(q)
## [1] "logical"
Character
# creates a vector of character values
r <- "Data"
class(r)
## [1] "character"
Vector can only store object of the same type. Storing multiple types of objects will convert them to a single type.
my_vec <- c("My", 10)
my_vec
## [1] "My" "10"
As we can see, 10
has been converted from a numeric type to a character type as seen in the output surounded by quotation mark.
1.6 List
List are created using the list()
function. List, as opposed to vector, can store objects of multiple types.
my_list <- list(42, "cd", FALSE, 1 + 3i)
my_list
## [[1]]
## [1] 42
##
## [[2]]
## [1] "cd"
##
## [[3]]
## [1] FALSE
##
## [[4]]
## [1] 1+3i
1.7 Matrices
Matrices are 2 dimensional vector which can only accept a single or objects of the same type.
Matrix
my_matrix <- matrix(1:6, nrow = 3, ncol = 2) # ":" stands for 1 to 6
my_matrix
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
Matrices have various attribute associated to it.
- Dimension –
dim()
- Number of Rows –
nrow()
- Number of Columns ==
ncol()
- Row names –
rownames()
- Column Names –
colnames
- Structure of the data –
str()
- Attributes –
attributes()
Dimension
dim(my_matrix)
## [1] 3 2
Number of Rows
nrow(my_matrix)
## [1] 3
Number of Columns
ncol(my_matrix)
## [1] 2
Row Names
rownames(my_matrix)
## NULL
Column Names
colnames(my_matrix)
## NULL
Structure
str(my_matrix)
## int [1:3, 1:2] 1 2 3 4 5 6
Attributes
attributes(my_matrix)
## $dim
## [1] 3 2
To extract certain elements from a Matrices, we have to use the square bracket [,]
instead.
Anything to the left of the ","
is to extract the rows.
Anything to the right of the ","
is to extract the columns.
Extracts 2nd column
my_matrix[,2]
## [1] 4 5 6
Extracts 1st column
my_matrix[,1]
## [1] 1 2 3
Extracts 2nd row
my_matrix[2,]
## [1] 2 5
Extracts 1st row
my_matrix[1,]
## [1] 1 4
Extracts the intersection of 3rd row and 2nd column
my_matrix[3,2]
## [1] 6
1.8 Data Frame
Data Frame are 2 dimensional list. They can store multiple types of objects as opposed to matrix
Data Frame
my_dataframe <- data.frame(name = c("Ali", "Bala", "Chong", "Dom"),
score = c(65, 57, 89, 92))
my_dataframe
## name score
## 1 Ali 65
## 2 Bala 57
## 3 Chong 89
## 4 Dom 92
Data Frame also have various attribute associated to it.
- Dimension –
dim()
- Number of Rows –
nrow()
- Number of Columns ==
ncol()
- Row names –
rownames()
- Column Names –
colnames
- Structure of the data –
str()
- Attributes – `attributes()
Dimension
dim(my_dataframe)
## [1] 4 2
Number of Rows
nrow(my_dataframe)
## [1] 4
Number of Columns
ncol(my_dataframe)
## [1] 2
Row Names
rownames(my_dataframe)
## [1] "1" "2" "3" "4"
Column Names
colnames(my_dataframe)
## [1] "name" "score"
Structure
str(my_dataframe)
## 'data.frame': 4 obs. of 2 variables:
## $ name : Factor w/ 4 levels "Ali","Bala","Chong",..: 1 2 3 4
## $ score: num 65 57 89 92
Attributes
attributes(my_dataframe)
## $names
## [1] "name" "score"
##
## $row.names
## [1] 1 2 3 4
##
## $class
## [1] "data.frame"
To extract certain elements from a Data Frame, we have to use the square bracket [,]
too
Anything to the left of the ","
is to extract the rows.
Anything to the right of the ","
is to extract the columns.
Extracts 2nd column
my_dataframe[,2]
## [1] 65 57 89 92
Extracts 1st column
my_dataframe[,1]
## [1] Ali Bala Chong Dom
## Levels: Ali Bala Chong Dom
Extracts 2nd row
my_dataframe[2,]
## name score
## 2 Bala 57
Extracts 1st row
my_dataframe[1,]
## name score
## 1 Ali 65
Extracts the intersection of 3rd row and 2nd column
my_dataframe[3,2]
## [1] 89
1.9 Packages
Most of time you will be using various 3rd party packages that have ben develop by other users in the community. Inorder to use these packages, we first have to install them. we can do that with the install.packages()
function.
# installing packages
install.packages("ggplot2")
Once the packages have been installed, we will have to load it into rstudio b4 we can use its functions. we can load it by calling the library()
function
# load the package into R studio
library(ggplot2)
1.10 Control Structures
In R, there are a few types of control structure.
They are mainly:
- Decision Making
- if … else statement
- nested if … else statement
- Loops
- While Loop
- Combination of both
1.10.1 Decision Making
if … else Statement
# First, create a variable
age <- 15
if (age < 21){
print("You are NOT an adult yet!")
} else {
print("You ARE an adult yet!")
}
## [1] "You are NOT an adult yet!"
Nested if … else Statement
if (age < 12) {
print("You are a children!")
} else if (age < 21) {
print("You are a teenager!")
} else {
print("You are an adult!")
}
## [1] "You are a teenager!"
1.10.2 Loops
While loop
# First, create a variable
age <- 15
# Execute the print statements as long as the condition is fulfilled
while (age < 21) {
print(age)
print("You are not an adult yet!")
age <- age + 1
}
## [1] 15
## [1] "You are not an adult yet!"
## [1] 16
## [1] "You are not an adult yet!"
## [1] 17
## [1] "You are not an adult yet!"
## [1] 18
## [1] "You are not an adult yet!"
## [1] 19
## [1] "You are not an adult yet!"
## [1] 20
## [1] "You are not an adult yet!"
1.10.3 Combining Decision making with loops.
# First, create a variable
age <- 8
while (age < 21) {
print(age)
if (age < 12) {
print("You are a children!")
} else {
print("You are a teenager!")
}
age <- age + 1
}
## [1] 8
## [1] "You are a children!"
## [1] 9
## [1] "You are a children!"
## [1] 10
## [1] "You are a children!"
## [1] 11
## [1] "You are a children!"
## [1] 12
## [1] "You are a teenager!"
## [1] 13
## [1] "You are a teenager!"
## [1] 14
## [1] "You are a teenager!"
## [1] 15
## [1] "You are a teenager!"
## [1] 16
## [1] "You are a teenager!"
## [1] 17
## [1] "You are a teenager!"
## [1] 18
## [1] "You are a teenager!"
## [1] 19
## [1] "You are a teenager!"
## [1] 20
## [1] "You are a teenager!"