Class activity solutions

Author

Ciaran Evans

x <- rep(NA, 100)
x <- rep(0, 3)
x <- seq(0, 3, 0.01)
x <- seq(0, 3, 0.01)
x[23]
[1] 0.22
x <- seq(0, 3, 0.01)
sqrt_x <- rep(NA, length(x))
for(i in 1:length(x)){
  sqrt_x[i] <- sqrt(x[i])
}
x <- seq(0, 3, 0.01)
sqrt_x <- sqrt(x)
x <- sample(1:100, 10, replace=FALSE)
x <- sample(-2:2, 10, replace=TRUE)
x <- rnorm(1000, mean=3, sd=sqrt(11))
exp_model <- function(n, beta0, beta1){
  x <- rnorm(n)
  y <- beta0 * x^beta1
  plot(x, y)
}
exp_model <- function(n, beta0, beta1){
  x <- rnorm(n)
  y <- beta0 * x^beta1
  return(list(x, y))
}
  1. There are many possible solutions; here is one example:
x <- list("a", list("a", c(2, 7, 9)))
x[[2]][[2]]
[1] 2 7 9
  1. Because x[1] is a list (containing a function), not a function

x <- list(function(n){return(rnorm(n, mean=0, sd=3))},
          function(n){return(runif(n, min=0, max=1))})

x[[1]](10)
 [1] -2.09638329 -1.08370156 -3.68404755  3.75592101  6.01960072  0.29506038
 [7]  0.05672882  4.32535667 -2.88014427 -2.68932223