ints = 1:5 # integer vector
ints
#> [1] 1 2 3 4 5
typeof(ints)
#> [1] "integer"More About Vectors
Reading Assignments
Make sure to read the following sections in the textbook: R Coding Basics, https://www.gastonsanchez.com/R-coding-basics/
The colon operator “:”, seq(), rep()
Colon operator “:”
- Generate integer vector, if starting and ending values are whole numbers
- Generate double vector, if starting value is not whole number
dbls = 5.2:1.2 # descending order; double vector
dbls
#> [1] 5.2 4.2 3.2 2.2 1.2
typeof(dbls)
#> [1] "double"- one-unit step
1.1:5
#> [1] 1.1 2.1 3.1 4.1seq() function
Below are equivalent
seq(from = 1, to = 5)seq(from = 1, to = 5, by = 1)seq(1, 5)seq(1, 5, 1)1:5
Odd numbers between
1to10
seq(from = 1, to = 10, by = 2)
#> [1] 1 3 5 7 9- The first
10odd numbers
seq(from = 1, by = 2, length.out = 10) # Total length of the vector is 10
#> [1] 1 3 5 7 9 11 13 15 17 19rep() function
timesvseach
rep(c(1,2), times = 3) # Repeat the entire vector 3 times
#> [1] 1 2 1 2 1 2
rep(c(1,2), each = 2) # Each element repeated twice
#> [1] 1 1 2 2length.out– set the total length
rep(c(1,2), length.out = 5) # Repeat the vector until reaching the total length
#> [1] 1 2 1 2 1- If
times&eachare both specified
rep(c(3,2,1), times = 3, each = 2) # "each" is performed first.
#> [1] 3 3 2 2 1 1 3 3 2 2 1 1 3 3 2 2 1 1timesequals a vector
rep(1:3, times = c(4, 5, 6))
#> [1] 1 1 1 1 2 2 2 2 2 3 3 3 3 3 3# Note that, using each doesn't work
rep(1:3, each = c(4, 5, 6)) Vector Coercion
Hierarchy Rule:
character > double > integer > logical
- characters have priority over other data types: as long as one element is a character, all other elements are coerced into characters
mixed <- c(TRUE, 1L, 2.0, "three")
mixed
#> [1] "TRUE" "1" "2" "three"Recall v1 – v4 from Lecture 2
# v1 is a logical vector
v1 <- c(FALSE, TRUE)
typeof(v1)
#> [1] "logical"
# v2 is an integer vector
v2 <- c(0L, 1L)
typeof(v2)
#> [1] "integer"
# v3 is a double vector
v3 <- c(0, 1)
typeof(v3)
#> [1] "double"
# v4 is a character vector
v4 <- c('0', '1') # Equivalently, v4 <- c("0", "1")
typeof(v4)
#> [1] "character"Coercion Examples
v5 <- c(v1, v2)
typeof(v5)
#> [1] "integer"
v6 <- c(v5, v3)
typeof(v6)
#> [1] "double"
v7 <- c(v6, v4)
typeof(v7)
#> [1] "character"Vectorization
- Vectorization is essential in R. Many operations in R are vectorized.
- Vectorized operations are essentially operations that are performed in an element by element fashion.
x = c(1, 2, 3, 4, 5)
y = c(5, 4, 3, 2, 1)
x + y
#> [1] 6 6 6 6 6c(1 + 5, 2 + 4, 3 + 3, 4 + 2, 5 + 1)Vectorizationvsfor loop- In other languages (e.g. C, python), vectorization is not typically a native feature.
Example 4.2
deposit <- 1000
rate <- 0.02
years <- 1:10 # vector of years
amounts <- deposit * (1 + rate)^yearsInput: years Output: amounts Function: deposit * (1 + rate)^years \(\leftarrow\) the code is vectorized
years
#> [1] 1 2 3 4 5 6 7 8 9 10
amounts
#> [1] 1020.000 1040.400 1061.208 1082.432 1104.081 1126.162 1148.686 1171.659
#> [9] 1195.093 1218.994Other examples of vectorized operator
sqrt(amounts)
#> [1] 31.93744 32.25523 32.57619 32.90034 33.22771 33.55834 33.89227 34.22951
#> [9] 34.57011 34.91410
log(amounts)
#> [1] 6.927558 6.947361 6.967163 6.986966 7.006768 7.026571 7.046374 7.066176
#> [9] 7.085979 7.105782Examples of not-vectorized operator
max(amounts)
#> [1] 1218.994
mean(amounts)
#> [1] 1116.872
length(amounts)
#> [1] 10Recycling
Takes the short vector, recycles its content to matches the longer vector.
c(1, 1, 2, 2, 3, 3) + c(0.5, 0.1)
#> [1] 1.5 1.1 2.5 2.1 3.5 3.1
c(1, 2, 3) + c(0.5, 0.1) # Warning Message!
#> [1] 1.5 2.1 3.5Subsetting
Creating the vector
# Example from Exercise 2.7
rates <- c(0.02, 0.03, 0.04)
names(rates) <- c("savings", "market", "certif")
# Equivalently
rates <- c(
"savings" = 0.02,
"market" = 0.03,
"certif" = 0.04
)Subsetting
Bracket Notation: []
Use (square) brackets [ ] to get access to the elements of a vector.
rates[2] # The second element
#> market
#> 0.03
rates[2:3] # The second to third elements
#> market certif
#> 0.03 0.04
rates[-3] # All elements except the 3rd element
#> savings market
#> 0.02 0.03
rates[c(1,2)] # The 1st and 2nd elements
#> savings market
#> 0.02 0.03
rates["certif"] # The element with name "certif"
#> certif
#> 0.04
rates[c("savings", "certif")]
#> savings certif
#> 0.02 0.04
rates[rates > 0.02] # All elements greater than 0.02
#> market certif
#> 0.03 0.04
rates[rates <= 0.03] # All elements less than or equal to 0.03
#> savings market
#> 0.02 0.03
rates[rates != 0.04] # All elements not equal to 0.04
#> savings market
#> 0.02 0.03