--- title: "Homework 7: Loops, Functions, and Apply" author: "KEY" date: "Due: May 17" output: html_document: preserve_yaml: true toc: true toc_float: true published: false --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) library(dplyr) library(tidyr) library(readr) library(ggplot2) ``` # 1. For and While Loops ### Question 1.1: Write in words the difference between a `for` loop and a `while` loop. > ANSWER: Both for and while loops allow for applying code iteratively. A for loop runs through a pre-specified set of indices, but a while loop runs until some condition is met. ### Question 1.2: Preallocate a matrix of NAs with 3 rows and 8 columns, called `double_matrix`. Manually specify the first column equal to the values 1, 2, and 3. Using a nested loop, fill in the matrix, row by row, such that each value is double that to its left. For example, the first row of the matrix should be: `r c(1,2,4,8,16,32,64,128)` ```{r} double_matrix <- matrix(NA,nrow=3,ncol=8) double_matrix[,1] <- 1:3 for(row in 1:3){ for(col in 2:8){ double_matrix[row,col] <- 2 * double_matrix[row,col-1] } } print(double_matrix) ``` ### Question 1.3: Ask ChatGPT the following question: "Write a while loop in R that samples the integers randomly between 1 and 100 until the number 50 is selected. Print how many iterations it took." Then, display the code ChatGPT wrote, say if it worked or not, and describe the steps ChatGPT took to reach a solution! (If it does not run, change the chunk option to `eval=FALSE`). ChatGPT can be accessed at chat.openai.com. ```{r eval=TRUE} count <- 0 # initialize counter to zero num <- 0 # initialize num to a non-50 value while (num != 50) { # continue loop until num is 50 count <- count + 1 # increment counter num <- sample(1:100, 1) # sample a random integer between 1 and 100 } cat("Number of iterations:", count) ``` > ANSWER: The code runs! ChatGPT initialized with a counter variable that counts the number of iterations that are run, as well as a number variable which stores the sampled number. The while loop runs until the number 50 is sampled, and then prints the number of iterations run in a nice format. ### Question 1.4: Write a while loop that keeps "flipping 5 coins" until you get 5 heads. Have the function count how many sets of 5 coin flips it took until getting 5 heads, and return that number. *Hint:* You can flip 5 coins using the code `rbinom(n=1,size=5,prob=.5)`. You have gotten 5 heads when the output is `5`. ```{r} counter <- 0 num_heads <- 0 while(num_heads < 5){ counter <- counter + 1 num_heads <- rbinom(n=1,size=5,prob=.5) } print(counter) ``` # 2. Functions ### Question 2.1: Pick a built-in R function and describe its input(s) and output(s). > ANSWER: The function `summary` takes an object as an input, such as a number, vector, matrix, or data.frame. It then outputs a summary of the object as best its able. Oftentimes, it will summarize matrices or data.frames by column. Output often includes the min, first quartile, median, mean, third quartile, and max. ### Question 2.2: Write a function, called `doubler`, that takes a single number as its input and returns a vector of length 8. The vector should contain the initial value, doubled consecutive times. Be sure to check if a single number was given! *Hint:* If input is 1, output should be `c(1,2,4,8,16,32,64,128)`. ```{r} doubler <- function(x){ ## Solution 1: vals <- c(x) for(i in 1:7){ x <- x*2 vals <- c(vals,x) } return(vals) ## Solution 2: # vals <- c(x,x*2,x*4,x*8,x*16,x*32,x*64,x*128) # return(vals) ## Solution 3: # vals <- x*2^(0:7) # return(vals) } ``` ### Question 2.3: Use `doubler` on the inputs 1, 2, and 3. Do you get the same values as in question 1.2? (You should!) ```{r} doubler(1) doubler(2) doubler(3) ``` # 3. Apply ### Question 3.1 Write an `apply()` statement to take the median value of each column in the `cars` dataset. *Hint: Use the `median()` function inside your `apply()` command!* ```{r} median_cars <- apply(cars,2,median) median_cars ``` ### Question 3.2 Using `ggplot`, make a scatterplot of the `speed` and `dist` variables in `cars`. Then, add an appropriate horizontal and vertical line symbolizing the median value of each variable. *Hint:* Using the layers `geom_vline(xintercept = )` and `geom_hline(yintercept = )` ```{r} ggplot(cars,aes(speed,dist))+geom_point()+ geom_vline(xintercept = median_cars[1])+ geom_hline(yintercept = median_cars[2]) ```