17.6 Test your R might!

  1. Using a loop, create 4 histograms of the weights of chickens in the ChickWeight dataset, with a separate histogram for time periods 0, 2, 4 and 6.

  2. The following is a dataframe of survey data containing 5 questions I collected from 6 participants. The response to each question should be an integer between 1 and 5. Obviously, we have some invalid values in the dataframe. Let’s fix them. Using a loop, create a new dataframe called survey.clean where all the invalid values (those that are not integers between 1 and 10) are set to NA.

survey <- data.frame("q1" = c(5, 3, 2, 7, 11, 5),
                     "q2" = c(4, 2, 2, 5, 5, 2),
                     "q3" = c(2, 1, 4, 2, 9, 10),
                     "q4" = c(2, 5, 2, 5, 4, 2),
                     "q5" = c(1, 4, -20, 2, 4, 2))

Here’s how your survey.clean dataframe should look:

# The cleaned survey data
survey.clean
##   q1 q2 q3 q4 q5
## 1  5  4  2  2  1
## 2  3  2  1  5  4
## 3  2  2  4  2 NA
## 4  7  5  2  5  2
## 5 NA  5  9  4  4
## 6  5  2 10  2  2
  1. Now, again using a loop, add a new column to the survey.clean dataframe called invalid.answers that indicates, for each participant, how many invalid answers they gave (Note: You may wish to use the is.na() function).

  2. Standardizing a variable means subtracting the mean, and then dividing by the standard deviation. Using a loop, create a new dataframe called survey.z that contains standardized versions of the columns in the following survey.B dataframe.

survey.B <- data.frame("q1" = c(5, 3, 2, 7, 1, 9),
                     "q2" = c(4, 2, 2, 5, 1, 10),
                     "q3" = c(2, 1, 4, 2, 9, 10),
                     "q4" = c(10, 5, 2, 10, 4, 2),
                     "q5" = c(4, 4, 3, 2, 4, 2))

Here’s how your survey.B.z dataframe should look:

survey.B.z
##      q1    q2    q3    q4    q5
## 1  0.16  0.00 -0.69  1.22  0.85
## 2 -0.49 -0.61 -0.94 -0.14  0.85
## 3 -0.81 -0.61 -0.17 -0.95 -0.17
## 4  0.81  0.30 -0.69  1.22 -1.19
## 5 -1.14 -0.91  1.12 -0.41  0.85
## 6  1.46  1.83  1.37 -0.95 -1.19