Extra Review of the First Material

Statistical Computing, 36-350

Week of Monday July 8, 2019

Getting Start:

Vectors

x <- c(1, 2, 3, 4, 5)
# x <- another way
# x <- another another way
set.seed(1)
x2 <- sample(c(TRUE, FALSE), p = c(1/3, 2/3), size = 10, replace = T)
x2
##  [1] FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE
# give x2 names c("X1", ... "X8")
x3 <- c("I", "took", "this", "class", "back", "in", "my", "day")

Matrices

x_mat <- matrix(x, nrow = 1)
dim(x_mat)
## [1] 1 5
x_mat2 <- matrix(c(1,1,2,3,5,8,
                   2,4,8,16,32,64,
                   0,1,0,-1,0,1),
            nrow = 3, byrow = TRUE)
x_mat2
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    1    2    3    5    8
## [2,]    2    4    8   16   32   64
## [3,]    0    1    0   -1    0    1
x2_mat <- matrix(x2, nrow = 5)
x3_mat <- matrix(x3, nrow = 2)

List

  1. make a list of x2, x_mat2 (my_list_1)
  2. name the elements in this list (my_list_1)
  3. make a list with all vectors above (my_list_2)

data.frame 1. make a data frame with all vector components… (my_df) 2. shorten them so you can (my_df) make a matrix with just x_mat2 in it. What will the columns names be? (my_df_2)

Vector Duplication

  1. add to x1 1 if the index is odd and -1 if the index is even
  2. override x2 entries so that if they’re false - they’re now true if the index is even (else leave as is)

Boolean Logic

  1. which indices for x1 are greater than 5?
  2. which indices of x_mat2 are greater than 5? Comment on format?

Indexing

  1. let’s talk about which data types use: [], [,], [[]], $
  2. what are the 3 ways to index into things
  3. take the third element of x2 (what are all the ways we could do this?)
  4. How do we grab value 16 out of x_mat2 (what are all the ways we could do this?)
  5. how can we grab the second column from my_df
  6. make those values > 5 in x_mat2 equal to 0

Iteration

  1. print out each element of x2 individually
  2. write the fibinaci sequence using a for-loop
  3. write it with a while loop
  4. create a print out of the binomial expansion