for
loop and a while
loop.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.eval=FALSE
). ChatGPT can be accessed at
chat.openai.com.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!doubler
on the inputs 1, 2, and 3. Do
you get the same values as in question 1.2? (You should!)apply()
statement to take the
median value of each column in the cars
dataset.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.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.
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: 1, 2, 4, 8, 16, 32, 64, 128
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)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 1 2 4 8 16 32 64 128
## [2,] 2 4 8 16 32 64 128 256
## [3,] 3 6 12 24 48 96 192 384
eval=FALSE
). ChatGPT can be accessed at
chat.openai.com.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)
## Number of iterations: 18
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.
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
.
counter <- 0
num_heads <- 0
while(num_heads < 5){
counter <- counter + 1
num_heads <- rbinom(n=1,size=5,prob=.5)
}
print(counter)
## [1] 64
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.
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)
.
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)
}
doubler
on the inputs 1, 2, and 3. Do
you get the same values as in question 1.2? (You should!)doubler(1)
## [1] 1 2 4 8 16 32 64 128
doubler(2)
## [1] 2 4 8 16 32 64 128 256
doubler(3)
## [1] 3 6 12 24 48 96 192 384
apply()
statement to take the
median value of each column in the cars
dataset.Hint: Use the median()
function inside your
apply()
command!
median_cars <- apply(cars,2,median)
median_cars
## speed dist
## 15 36
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 = )
ggplot(cars,aes(speed,dist))+geom_point()+
geom_vline(xintercept = median_cars[1])+
geom_hline(yintercept = median_cars[2])