Resources
Things to know
Operators
Assignment:x <- 9
Vectors
Think: list
Create a vector:x <- c(2, 3, 5, 7)
[1] 2 3 5 7
Shortcut to create a sequential vector:x <- 1:10
[1] 1 2 3 4 5 6 7 8 9 10
Applying arithmetic and functions to a vector applies to each element.
Number of elements:length(x)
[1] 10
Matrices
Think: table
Create a matrix:y = matrix(1:10, nrow=2, ncol=5)
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
Access an entry in a matrix:y[1, 2]
[1] 3
Sum over rows:rowSums(y)
[1] 25 30
Sum over columns:colSums(y)
[1] 3 7 11 15 19
Size of matrix:dim(y)
[1] 2 5
Random
Randomly sample elements:x <- 1:10
sample(x, 3)
[1] 2 10 5
Randomly sample elements, replace (i.e., can sample again) after sampling:sample(x, 3, replace=TRUE)