CSC121 - R Console Monday February 26th --------------------------- > # Different ways to create vectors > # Repeating values: we can make a vector by repeating values a set number of times > # We use the function rep() > rep(5, 10) [1] 5 5 5 5 5 5 5 5 5 5 > # This repeats the value 5 ten times > rep(c(8, 1, 2), 5) [1] 8 1 2 8 1 2 8 1 2 8 1 2 8 1 2 > rep(c("hi", "csc121"), 3) [1] "hi" "csc121" "hi" "csc121" "hi" "csc121" > # Instead of saying how many times to repeat, you can instead say what the final length should be > rep(c(8, 1, 2), length=10) [1] 8 1 2 8 1 2 8 1 2 8 > # this will continue repeating the values until the desired length is reached > # Another option is to say how many times each element should be repeated immediately > rep(c(8, 1, 2), each=3) [1] 8 8 8 1 1 1 2 2 2 > # This repeats each element in the vector 3 times > > # Sequence vectors > # We've seen that you can create a vector consisting of a sequence of consecutive integers > 1:20 [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 > # If we make the starting number larger than the ending number, we get a sequence that decreases > 20:1 [1] 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 > > # All of these sequences increment by 1 > # The seq() function allows us to create a sequence of numbers that increment by an amount other than 1 > seq(1, 2, by=0.1) [1] 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 > # this creates a sequence from 1 to 2, but increments by 0.1 > > seq(1, 30, by=5) [1] 1 6 11 16 21 26 > # It will start at the starting number, but it will not go above the ending number > > # We can also specify the desired length of the sequence instead of an ending number: > seq(1.1, by=0.01, length=13) [1] 1.10 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.18 1.19 1.20 1.21 1.22 > # this starts at 1.1 and increments by 0.01 until we get to a vector of length 13 > > # You can combine normal a:b seuqnces, rep(), seq(), to create different combinations of vectors > c(1:5, 5:1) [1] 1 2 3 4 5 5 4 3 2 1 > rep(1:5, 3) [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 > c(seq(1, 2, by=0.2), rep(2, 5)) [1] 1.0 1.2 1.4 1.6 1.8 2.0 2.0 2.0 2.0 2.0 2.0 > seq(1:5, by=0.01) Error in seq.default(1:5, by = 0.01) : 'from' must be of length 1 > > > # MATRICES > > # A matrix is a data structure that has m rows and n columns > # They are made using the matrix() function > # Let's try putting in one value > matrix(4) [,1] [1,] 4 > # A default matrix has one row and one column > # To make a multi-row/column matrix, we specify the data, the number of rows, and the number of columns > matrix(3, nrow=2, ncol=2) [,1] [,2] [1,] 3 3 [2,] 3 3 > # The data repeats until the matrix is filled > # If we have a set of more than one value as the data, it will fill them in column by column > matrix(1:6, nrow=2, ncol=3) [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 > # You can choose to fill the data in by rows if you want by specificying the 'byrow' argument > matrix(1:6, nrow=2, ncol=3, byrow=TRUE) [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 > > # Matrix operations > A <- matrix(1:4, nrow=2, ncol=2) > A [,1] [,2] [1,] 1 3 [2,] 2 4 > # Matrix addition > A + A [,1] [,2] [1,] 2 6 [2,] 4 8 > # The operations we've seen before work on matrices element-by-element > -A [,1] [,2] [1,] -1 -3 [2,] -2 -4 > A^2 [,1] [,2] [1,] 1 9 [2,] 4 16 > > # There are 'matrix operations' which come to us from linear algebra > # For example, matrix multiplication > B <- matrix(c(4, 0, 3, 1), nrow=2, ncol=2) > # We can perform matrix multiplication using the %*% > A %*% B [,1] [,2] [1,] 4 6 [2,] 8 10 > A * B [,1] [,2] [1,] 4 9 [2,] 0 4 > # First one multiplies using matrix multiplication (review your linear algrebra) > # second one multiplies element-by-element > > # You can think of matrices simply as another way to organize values of any type > matrix(c(TRUE, FALSE), nrow=2, ncol=2) [,1] [,2] [1,] TRUE TRUE [2,] FALSE FALSE > matrix(c("hi", 4), nrow=2, ncol=2) [,1] [,2] [1,] "hi" "hi" [2,] "4" "4" > matrix(list("hi", 4), nrow=2, ncol=2) [,1] [,2] [1,] "hi" "hi" [2,] 4 4 > # You can have multiple types in a matrix, just make sure you put them in a list when creating the matrix - vectors will convert everything to one type > # R will warn you if the data you're using isn't a multiple of the row number > matrix(1:4, nrow=3, ncol=6) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 4 3 2 1 4 [2,] 2 1 4 3 2 1 [3,] 3 2 1 4 3 2 Warning message: In matrix(1:4, nrow = 3, ncol = 6) : data length [4] is not a sub-multiple or multiple of the number of rows [3] > > # Indexing matrix elements > # To get an element of a matrix, you must specify the row and column > X <- matrix(1:6, nrow=2, ncol=3) > X [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 > # You will need two indexes/subscripts. First the row, then the column > X[1, 3] [1] 5 > X[2, 1] [1] 2 > # You can index an entire row or column of a matrix > X[1,] [1] 1 3 5 > # ^ we got the first row > # Get the second column: > X[,2] [1] 3 4 > X[1,] + X[,2] [1] 4 7 8 Warning message: In X[1, ] + X[, 2] : longer object length is not a multiple of shorter object length > X[1,] + X[2,] [1] 3 7 11 > # Can find the number of rows and columns > nrow(X) [1] 2 > ncol(X) [1] 3 > # Length tells you the number of elements: > length(X) [1] 6 >