Chapter 20 Challenge 3

You have realized that another of your tools was not working properly!

At Mississippi sites, concentrations less than 300 were measured correctly, but concentrations equal or higher than 300 were overestimated by 20 units!

Your mission is to use a loop to correct these measurements for all Mississippi sites.

Tip. Make sure you reload the \(\text{CO}_{2}\) data so that we are working with the raw data for the rest of the exercise:

data(CO2)

Challenge 3: Answer

for (i in 1:nrow(CO2)) {
    if (CO2$Type[i] == "Mississippi") {
        if (CO2$conc[i] < 300)
            next
        CO2$conc[i] <- CO2$conc[i] - 20
    }
}

We could also have written it in this way, which is more concise and clearer.

for (i in 1:nrow(CO2)) {
    if (CO2$Type[i] == "Mississippi" && CO2$conc[i] >= 300) {
        CO2$conc[i] <- CO2$conc[i] - 20
    }
}