Block 5: Missing values and a first plot

Week 2 · Introduction to R · Imai §1.3.5 · about 13 min

NoteHow this works

Read the code and predict (write it down or tell your neighbour), answer the question, then run it with Run Code. Every cell is editable; Start Over resets it. Errors are information: read them.

1. Predict: missing values

What does each line return? Write down your guess, then run.

Read this code (it is not run yet):

UNpop <- read.csv("UNpop.csv")
world.pop.na <- c(UNpop$world.pop, NA)
world.pop.na
mean(world.pop.na)
mean(world.pop.na, na.rm = TRUE)
Predict: What does mean(c(UNpop$world.pop, NA)) return?
Most functions return NA if any input is NA. na.rm = TRUE ignores it, but that is a substantive choice.

Now run it and compare with your answer:

Tip

Always ask: why is a value missing, and does removing it change the substantive answer?

2. A first plot

3. Your turn

  1. Add a title with main = "..." and change the point style with pch = 16.
  2. Plot the percent increase from Block 2 against the decade start (year[-7]). What does this picture show that the population plot hides?
# For (a): add  main = "World population", pch = 16  inside plot(...)
# For (b): plot(year[-7], percent.increase, type = "b")
UNpop <- read.csv("UNpop.csv") par(mfrow = c(1, 2)) plot(UNpop$year, UNpop$world.pop / 1e6, type = "b", pch = 16, main = "World population", xlab = "Year", ylab = "World population (billions)") plot(year[-7], percent.increase, type = "b", pch = 16, main = "Growth per decade", xlab = "Decade start", ylab = "Growth (%)")
UNpop <- read.csv("UNpop.csv")
par(mfrow = c(1, 2))
plot(UNpop$year, UNpop$world.pop / 1e6, type = "b", pch = 16,
     main = "World population",
     xlab = "Year", ylab = "World population (billions)")
plot(year[-7], percent.increase, type = "b", pch = 16,
     main = "Growth per decade",
     xlab = "Decade start", ylab = "Growth (%)")