= 1:5 # integer vector
ints
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
= 5.2:1.2 # descending order; double vector
dbls
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.1
seq()
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
1
to10
seq(from = 1, to = 10, by = 2)
#> [1] 1 3 5 7 9
- The first
10
odd 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 19
rep()
function
times
vseach
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 2
length.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
&each
are 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 1
times
equals 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
<- c(TRUE, 1L, 2.0, "three")
mixed
mixed#> [1] "TRUE" "1" "2" "three"
Recall v1
– v4
from Lecture 2
# v1 is a logical vector
<- c(FALSE, TRUE)
v1 typeof(v1)
#> [1] "logical"
# v2 is an integer vector
<- c(0L, 1L)
v2 typeof(v2)
#> [1] "integer"
# v3 is a double vector
<- c(0, 1)
v3 typeof(v3)
#> [1] "double"
# v4 is a character vector
<- c('0', '1') # Equivalently, v4 <- c("0", "1")
v4 typeof(v4)
#> [1] "character"
Coercion Examples
<- c(v1, v2)
v5 typeof(v5)
#> [1] "integer"
<- c(v5, v3)
v6 typeof(v6)
#> [1] "double"
<- c(v6, v4)
v7 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.
= c(1, 2, 3, 4, 5)
x = c(5, 4, 3, 2, 1)
y + y
x #> [1] 6 6 6 6 6
c(1 + 5, 2 + 4, 3 + 3, 4 + 2, 5 + 1)
Vectorization
vsfor loop
- In other languages (e.g. C, python), vectorization is not typically a native feature.
Example 4.2
<- 1000
deposit <- 0.02
rate <- 1:10 # vector of years
years <- deposit * (1 + rate)^years amounts
Input: 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.994
Other 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.105782
Examples of not-vectorized operator
max(amounts)
#> [1] 1218.994
mean(amounts)
#> [1] 1116.872
length(amounts)
#> [1] 10
Recycling
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.5
Subsetting
Creating the vector
# Example from Exercise 2.7
<- c(0.02, 0.03, 0.04)
rates names(rates) <- c("savings", "market", "certif")
# Equivalently
<- c(
rates "savings" = 0.02,
"market" = 0.03,
"certif" = 0.04
)
Subsetting
Bracket Notation: []
Use (square) brackets [ ]
to get access to the elements of a vector.
2] # The second element
rates[#> market
#> 0.03
2:3] # The second to third elements
rates[#> market certif
#> 0.03 0.04
-3] # All elements except the 3rd element
rates[#> savings market
#> 0.02 0.03
c(1,2)] # The 1st and 2nd elements
rates[#> savings market
#> 0.02 0.03
"certif"] # The element with name "certif"
rates[#> certif
#> 0.04
c("savings", "certif")]
rates[#> savings certif
#> 0.02 0.04
> 0.02] # All elements greater than 0.02
rates[rates #> market certif
#> 0.03 0.04
<= 0.03] # All elements less than or equal to 0.03
rates[rates #> savings market
#> 0.02 0.03
!= 0.04] # All elements not equal to 0.04
rates[rates #> savings market
#> 0.02 0.03