Instructions: Work with a neighbor to answer the following questions, then we will discuss the activity as a class. To get started, download the class activity template file.
So far, we have learned a lot about R and computing beyond what we saw in STA 112. We have covered vectors, random sampling, for
and while
loops, simulation studies, functions (including defaults and anonymous functions), and lists. The purpose of this class activity is to briefly review some of the core R tools we have covered. (This review is not fully comprehensive).
Create a vector containing NA
repeated 100 times.
Create a vector containing 0
repeated 3 times.
Create a vector containing the numbers 0, 0.01, 0.02,…, 3 (the sequence from 0 to 3, incrementing by 0.01).
What is the 23rd entry in the vector you created in question 3?
Take the square root of all entries of the vector in question 3, using a for
loop.
Take the square root of all entries of the vector in question 3, without using a for
loop.
Choose a random sample of 10 integers between 1 and 100, without replacement.
Choose a random sample of 10 integers between -2 and 2, with replacement.
Sample 1000 observations from a \(N(3, 11)\) distribution (11 is the variance, not the standard deviation).
n
, a slope beta1
, and an intercept beta0
n
, a slope beta1
, and an intercept beta0
Create a list x
such that the vector c(2, 7, 9)
can be extracted with x[[2]][[2]]
.
Explain why the following code causes an error:
x <- list(function(n){return(rnorm(n, mean=0, sd=3))},
function(n){return(runif(n, min=0, max=1))})
x[1](10)
## Error in eval(expr, envir, enclos): attempt to apply non-function