# Name: Ha Khanh Nguyen
# NetID: hknguyen
# Lab 1
# Exercise 1
roll_dice <- function(){
outcomes <- sample(x = 1:6, size = 6, replace = TRUE)
mean(outcomes)
}
# testing the function
roll_dice()
roll_dice()
average_dice <- replicate(n = 100000, expr = roll_dice())
hist(x = average_dice, breaks = seq(from = 0, to = 6, by = 0.5))
#---------
# Exercise 2
roll_dice_again <- function(red = 1:6, green = 1:6){
red_outcome <- sample(x = red, size = 1) # we don't need replace = TRUE here since sample size = 1
green_outcome <- sample(x = green, size = 1)
red_outcome >= green_outcome
}
# testing the function
roll_dice_again(red = 1:4, green = 1:3)
roll_dice_again()
roll_dice_again(red = 1:2)
roll_dice_again(green = 3:10)
#---------
# Exercise 3
instructor <- list("Ha", 24)
students <- list(list("Alex", 20), list("Dave", 21))
stat385 <- list(instructor, students)
stat385
# Get Alex’s age
stat385[[2]][[1]][[2]]
# Get a list containing ONLY Dave’s name and age
stat385[[2]][[2]]
# Get Ha’s name
stat385[[1]][[1]]
#---------
# Exercise 4
m <- matrix(data = 1:16, nrow = 4, byrow = TRUE)
m
# Get the element on the 1st row, 3rd column
m[1,3]
# Get the element on the 3rd row, 2nd column
m[3,2]
# Get all the elements from the 1st row
m[1, ]
# Get all the elements from the 3rd column
m[ ,3]
# Compute the sum of all the elements from the 1st row
mean(m[1, ])
# Compute the average of all the elements from the 3rd column
sum(m[, 3])