EDS 221 Day 3 AM

Conditions and logic


August 12th, 2026

Review day 2: Indexing


On a sheet of paper, draw the reef matrix after this code runs:

reef <- matrix(0, nrow = 5, ncol = 5)
coral_row <- 3
coral_col <- 4
reef[coral_row, coral_col] <- 1

Review day 2: Indexing


Recall the subgoals for evaluating indexing expressions

  1. Identify the object being indexed (vector, data frame, matrix, etc)
  2. Identify the index (inside [ ] or following $)
  3. Determine the type of the index (positive integers, negative integers, conditions, names, blanks) and evaluate it
  4. Apply the index to the object and determine the result

On a sheet of paper, apply these subgoals to evaluate the following code.

growth_row_offset <- c(-1, -1, -1, 0, 1, 1, 1, 0)
growth_col_offset <- c(-1, 0, 1, 1, 1, 0, -1, -1)

growth_roll <- 4

growth_row <- coral_row + 
  growth_row_offset[growth_roll]
growth_col <- coral_col + 
  growth_col_offset[growth_roll]
reef[growth_row, growth_col] <- 1