Environment

Work directory

setwd('~/Documents/R')

Packages installation

install.packages('tidyquant')

Load packages

library(tidyquant)
require(tidyquant)
#both applicable, but the latter is more commonly used

Detach packages

detach('package:tidyquant')

Import data

df <- read.table("WTI.csv", sep = ",")
df <- read.csv("WTI.csv")

Export data

write.table(df, file = "FX.csv", sep = ",") 
write.csv(df, file = "FX.csv")
#use write.table can output data in various type, like .txt, .csv etc

Mathematics

Arithmetics

1 + 2 + 3 # add / plus
6 - 3     # minus / subtract 
3 * 7     # times / multiple
120 / 3   # divide
120 / 7
4 * 6 * 5
(4 * 6) * 5
4 * (6 * 5)

Advanced arithmetics

6 ^ 2     # second power
## [1] 36
6 ** 2    # second power
## [1] 36
6 ^ 5     # six to the fifth power
## [1] 7776
sqrt(36)  # square root
## [1] 6
27 ^ (1/3) # cube root / third root of 27
## [1] 3
16 %/% 3  # quotient
## [1] 5
16 %% 3   # remainder
## [1] 1

Sign of a number

sign(10)  # print out +1 if positive
## [1] 1
sign(0)   # print out  0 if zero
## [1] 0
sign(-10) # print out -1 if negative
## [1] -1

Absolute value

abs(-10)
## [1] 10

Logarithm

log(10)   # based on e
## [1] 2.302585
log1p(9)  # log(x) = log1p(x - 1), log(10) = log1p(9)
## [1] 2.302585
log(10, 2) # based on 2
## [1] 3.321928
log10(10) # based on 10
## [1] 1

Exponential

exp(3)
## [1] 20.08554
expm1(3)  # expm1(x) = exp(x) - 1
## [1] 19.08554

Data type

Vector, Numeric, Character, Logic

a <- seq(from = 1, to = 20 , by = 2) # numeric
b <- letters[1:10] # character 
c <- rep(c(TRUE, FALSE), 5) # logic 
a; b; c
##  [1]  1  3  5  7  9 11 13 15 17 19
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
##  [1]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
class(a); class(b); class(c)
## [1] "numeric"
## [1] "character"
## [1] "logical"
# use class() to detect the data type

Dataframe, Matrix, List

d <- matrix(1:15, ncol = 3, nrow = 5, byrow = T)
e <- as.data.frame(d) # transform matrix into dataframe
f <- list(d,e) # make it as a list
d; e; f
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
## [4,]   10   11   12
## [5,]   13   14   15
##   V1 V2 V3
## 1  1  2  3
## 2  4  5  6
## 3  7  8  9
## 4 10 11 12
## 5 13 14 15
## [[1]]
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
## [4,]   10   11   12
## [5,]   13   14   15
## 
## [[2]]
##   V1 V2 V3
## 1  1  2  3
## 2  4  5  6
## 3  7  8  9
## 4 10 11 12
## 5 13 14 15
class(d); class(e); class(f)
## [1] "matrix"
## [1] "data.frame"
## [1] "list"

Common orders

t(d)   # transpose of a matrix
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    4    7   10   13
## [2,]    2    5    8   11   14
## [3,]    3    6    9   12   15
dim(d) # report dim of a matrix
## [1] 5 3
table(c) # compute data frequency
## c
## FALSE  TRUE 
##     5     5
summary(d) # summary statistic of data
##        V1           V2           V3    
##  Min.   : 1   Min.   : 2   Min.   : 3  
##  1st Qu.: 4   1st Qu.: 5   1st Qu.: 6  
##  Median : 7   Median : 8   Median : 9  
##  Mean   : 7   Mean   : 8   Mean   : 9  
##  3rd Qu.:10   3rd Qu.:11   3rd Qu.:12  
##  Max.   :13   Max.   :14   Max.   :15
head(d, n = 5L)  # print out the first 5(default) row of data
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
## [4,]   10   11   12
## [5,]   13   14   15
tail(d, n = 5L)  # print out the last 5(default) row of data
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
## [4,]   10   11   12
## [5,]   13   14   15
cbind(a, b, c)   # bind data via columns
##       a    b   c      
##  [1,] "1"  "a" "TRUE" 
##  [2,] "3"  "b" "FALSE"
##  [3,] "5"  "c" "TRUE" 
##  [4,] "7"  "d" "FALSE"
##  [5,] "9"  "e" "TRUE" 
##  [6,] "11" "f" "FALSE"
##  [7,] "13" "g" "TRUE" 
##  [8,] "15" "h" "FALSE"
##  [9,] "17" "i" "TRUE" 
## [10,] "19" "j" "FALSE"
rbind(a, b, c)   # bind data via rows
##   [,1]   [,2]    [,3]   [,4]    [,5]   [,6]    [,7]   [,8]    [,9]  
## a "1"    "3"     "5"    "7"     "9"    "11"    "13"   "15"    "17"  
## b "a"    "b"     "c"    "d"     "e"    "f"     "g"    "h"     "i"   
## c "TRUE" "FALSE" "TRUE" "FALSE" "TRUE" "FALSE" "TRUE" "FALSE" "TRUE"
##   [,10]  
## a "19"   
## b "j"    
## c "FALSE"
merge(d, e, by = "V1") # merge two data set, "by" means merge via which column
##   V1 V2.x V3.x V2.y V3.y
## 1  1    2    3    2    3
## 2  4    5    6    5    6
## 3  7    8    9    8    9
## 4 10   11   12   11   12
## 5 13   14   15   14   15

Data manipulation

d[1, 2]   # report the first row, second column of data
## [1] 2
d[, -2]   # delete values of the second column
##      [,1] [,2]
## [1,]    1    3
## [2,]    4    6
## [3,]    7    9
## [4,]   10   12
## [5,]   13   15
colnames(e); rownames(e)
## [1] "V1" "V2" "V3"
## [1] "1" "2" "3" "4" "5"
as.character(d[, 2]) # transform the specified data into character type
## [1] "2"  "5"  "8"  "11" "14"
head(paste(d[, 1], "%", sep = ",")) # deal with character binding
## [1] "1,%"  "4,%"  "7,%"  "10,%" "13,%"
head(round(d[, 1], 2)) # specified values rounded to 2 decimal places
## [1]  1  4  7 10 13

Simple loops

x <- 0
for( i in -10:10){ 
  x <- (i + i^2 - i^3) 
  print(x)
}
## [1] 1090
## [1] 801
## [1] 568
## [1] 385
## [1] 246
## [1] 145
## [1] 76
## [1] 33
## [1] 10
## [1] 1
## [1] 0
## [1] 1
## [1] -2
## [1] -15
## [1] -44
## [1] -95
## [1] -174
## [1] -287
## [1] -440
## [1] -639
## [1] -890
x <- -1
while(x < 5) {
  x = x + 1
  print(x)
}
## [1] 0
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5