Chapter 15 Challenge 2

You have realized that your tool for measuring uptake was not calibrated properly at Quebec sites and all measurements are 2 units higher than they should be.

  1. Use a loop to correct these measurements for all Qu├ębec sites.

  2. Use a vectorisation-based method to calculate the mean \(CO_2\) uptake in both areas.

For this, you must load the \(\text{CO}_{2}\) dataset using data(CO2), and then use the object CO2.

1.-Using for and if to correct the measurements:

for (i in 1:length(CO2[, 1])) {
    if (CO2$Type[i] == "Quebec") {
        CO2$uptake[i] <- CO2$uptake[i] - 2

2.- Using tapply() to calculate the mean for each group:

tapply(CO2$uptake, CO2$Type, mean)
##      Quebec Mississippi 
##    31.54286    20.88333