R intro

deeply based on vignette source 'RIntro.Rnw'

Wilson Freitas
Quant

Define where you are about to work

> getwd()
## [1] "/Users/wilson"
> setwd("/Users/wilson/Google Drive/dev/trading-strategies/slides")
> getwd()
## [1] "/Users/wilson/Google Drive/dev/trading-strategies/slides"

Creating numeric variables

> y <- 5  # y = 5 also work but <- is R stylish
> y
## [1] 5
> class(y)
## [1] "numeric"

Creating numeric vectors

> x <- c(3.1416, 2.7183, 1, 3)
> x
## [1] 3.142 2.718 1.000 3.000
> x[2]  # indexing
## [1] 2.718
> class(x)
## [1] "numeric"

Creating matrix variables

> m <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3)
> m
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
> class(m)
## [1] "matrix"

Creating data.frame variables

> tab <- data.frame(store = c("downtown", "eastside", "airport"), sales = c(32, 
+     17, 24))
> tab
##      store sales
## 1 downtown    32
## 2 eastside    17
## 3  airport    24
> class(tab)
## [1] "data.frame"

Creating string vectors (character type)

cities <- c("Seattle", "Portland", "San Francisco")
cities
## [1] "Seattle"       "Portland"      "San Francisco"
class(cities)
## [1] "character"

Checking who is out there

> ls()  # list variables in workspace (memory)
## [1] "cities" "f"      "m"      "tab"    "x"      "y"
> # cleaning up memory
> rm(list = ls())
> ls()
## character(0)

vectorized operations

> constants <- c(3.1416, 2.7183, 1.4142, 1.618)
> names(constants) <- c("pi", "euler", "sqrt2", "golden")  # naming vector elements
> constants
##     pi  euler  sqrt2 golden 
##  3.142  2.718  1.414  1.618
> constants^2  # squaring all elements
##     pi  euler  sqrt2 golden 
##  9.870  7.389  2.000  2.618

vector indexing

> c(constants[c(1, 3, 4)], constants[c(-1, -2)])
##     pi  sqrt2 golden  sqrt2 golden 
##  3.142  1.414  1.618  1.414  1.618
> constants[c("pi", "golden")]
##     pi golden 
##  3.142  1.618
> constants > 2
##     pi  euler  sqrt2 golden 
##   TRUE   TRUE  FALSE  FALSE
> constants[constants > 2]
##    pi euler 
## 3.142 2.718

recycling rule

> constants
##     pi  euler  sqrt2 golden 
##  3.142  2.718  1.414  1.618
> constants * 2
##     pi  euler  sqrt2 golden 
##  6.283  5.437  2.828  3.236
> constants * c(0, 1)
##     pi  euler  sqrt2 golden 
##  0.000  2.718  0.000  1.618
> constants * c(0, 1, 2)
## Warning: longer object length is not a multiple of shorter object length
##     pi  euler  sqrt2 golden 
##  0.000  2.718  2.828  0.000

sequences

> 1:5
## [1] 1 2 3 4 5
> -5:5
##  [1] -5 -4 -3 -2 -1  0  1  2  3  4  5
> seq(from = 0, to = 1, len = 5)
## [1] 0.00 0.25 0.50 0.75 1.00
> seq(from = 0, to = 20, by = 2.5)
## [1]  0.0  2.5  5.0  7.5 10.0 12.5 15.0 17.5 20.0

sequences

> seq(0, 10, 2)
## [1]  0  2  4  6  8 10
> seq(by = 2, 0, 10)
## [1]  0  2  4  6  8 10
> seq(0, 10, len = 5)
## [1]  0.0  2.5  5.0  7.5 10.0
> seq(0, 10)
##  [1]  0  1  2  3  4  5  6  7  8  9 10

rep function

> rep(0, 10)  # initialize a vector
##  [1] 0 0 0 0 0 0 0 0 0 0
> rep(1:4, 2)  # repeat pattern 2 times
## [1] 1 2 3 4 1 2 3 4
> rep(1:4, each = 2)  # repeat each element 2 times
## [1] 1 1 2 2 3 3 4 4
> rep(1:4, c(2, 1, 2, 1))
## [1] 1 1 2 3 3 4
> rep(1:4, each = 2, len = 10)  # 8 integers plus two recycled 1's.
##  [1] 1 1 2 2 3 3 4 4 1 1
> rep(1:4, each = 2, times = 3)  # length 24, 3 complete replications
##  [1] 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4