Lab 1
Labs Assignments
The purpose of this lab is to get you working with base R looping functions while also being careful about data types. Your are welcome to work with a partner or in a small group of 3-4 people.
Data structures
set.seed(123) # Set random number generation
m <- lm(mpg ~ hp + cyl + drat, mtcars)
l <- list(a = list(m = matrix(1:12, ncol = 3),
v = 1:7),
b = data.frame(student = 1:15,
score = rnorm(15, 100, 10)))
Run the above code
-
From the model results:
- Extract the qr tolerance (
tol). - Extract the term labels from the model.
- hint: Use
strto inspect the list. This one is tricky and involves pulling an attribute from an attribute.
- hint: Use
- Extract the qr tolerance (
-
From the list
- Use at least two different methods to extract
m. - Extract the third column of
m. Maintain the dimensions (matrix structure). - Extract the score for student 7.
- Use at least two different methods to extract
Loops
-
Use the “airquality” dataset, which is part of base R. Split it by
Month -
Use a
forloop to calculate the meanOzoneby month (remember to remove missing data from the calculation) -
Replicate this calculation with
lapply,sapply, andvapply -
Produce separate plots showing the relation between
OzoneandSolar.Rfor each month (with a single loop). -
Use a
forloop to save the plots to a folder on your computer