5.2 Example: Vector

Consider the following question from Homework 2.

Part 1: Coding Assignment

  1. (Public Question) Using the function sample, randomly sample with replacement numbers from key and save this vector as index. The length of index should be 3.

The answer should look something like:

index <- sample(key, size = 3, replace = TRUE)

Assume this question is worth 10 points and that key was created in Question 1.

Then, the autograder code for this question could look like:

#Autograder Code for Part 1 Question 2----------
  
#Initializing `test.results[2, ]`
test.results[2, ] <- c("Part 1 Question 2 (Public)", 0, 10, "Try again.")

#Prerequisite Check 
if(test.results[1, 2] == 0){
  test.results[2, 4] <- "This problem depends on Part 1 Question 1 being correct. Try again."
#Name Check 
} else if(is.error(index)){ 
  test.results[2, 4] <- "`index` is not found. Please make sure your variable is named correctly."
#Structure Check (for a vector)
} else if(!is.vector(index, mode = "any")){ 
  test.results[2, 4] <- "Please make sure `index` is a vector."
#Structure Check (for a numeric vector)
} else if(!is.vector(index, mode = "numeric")){ 
  test.results[2, 4] <- "Please make sure `index` is a numeric vector."
#Length Check (i.e., Row & Column Check for a vector)
} else if(length(index) != length(index_test)){ 
  test.results[2, 4] <- "Please make sure the vector `index` is the correct length, as specified by the prompt (i.e., there are 3 elements in the vector)."
#Value Check (for a vector)
} else if(!all(index %in% index_test)){ 
  test.results[2, 4] <- "`index` has incorrect values. Hint: did you use the argument replace = TRUE in the `sample()` function?"
#Correct Check
} else if(isTRUE(all.equal(index, index_test, 
                           tolerance = 0.001, check.attributes = F))){ 
  test.results[2, 4] <- "Well done!"
  test.results[2, 2] <- 10
}