Chapter 8 — Practice Demo Lab: Reference Solution

Lab: Combine three forestry sources

Note

Reference solution for the Chapter 8 Practice Demo Lab (Tasks 1–6 of Lab: Combine three forestry sources). The graded Canvas lab has its own private answer key.

Task 1 — Inventory

bc <- read_excel(here("data","bc_disturbance_reforestation.xlsx"), sheet="Data")
on <- read_excel(here("data","on_forest_statistics_1.xlsx"), sheet="Data")
air <- read_csv(here("data","airdata3.csv"), show_col_types = FALSE)
tibble(file = c("BC silviculture","Ontario forest stats","BC air quality"),
       rows = c(nrow(bc), nrow(on), nrow(air)),
       has_area_ha = c("Harvested_ha, …","SumOfTotalHa","—"),
       has_year    = c("Fiscal_Year","2021 snapshot","Date (hourly)")) |> kable()
file rows has_area_ha has_year
BC silviculture 37 Harvested_ha, … Fiscal_Year
Ontario forest stats 179 SumOfTotalHa 2021 snapshot
BC air quality 11904 Date (hourly)

Comparable across the two forestry files: a year and an area in hectares. The air-quality file has no area — it is measured on a different axis (hourly pollutant concentrations), so it is the odd one out.

Task 2 — Select comparable columns (uniform schema)

bc_h <- bc |> transmute(province = "British Columbia",
                        year = Fiscal_Year, area_ha = Harvested_ha)
on_h <- on |> transmute(province = "Ontario",
                        year = 2021L, area_ha = SumOfTotalHa)

Task 3 — Stack with bind_rows()

combined <- bind_rows(bc_h, on_h)
kable(bind_rows(head(bc_h, 2), head(on_h, 2)),
      caption = "Uniform schema: province, year, area_ha (first rows of each source).")
Uniform schema: province, year, area_ha (first rows of each source).
province year area_ha
British Columbia 1987 242182.000
British Columbia 1988 251557.320
Ontario 2021 2184.069
Ontario 2021 26085.574

Task 4 — Diagnose

count(combined, province)          # both provinces present?
# A tibble: 2 × 2
  province             n
  <chr>            <int>
1 British Columbia    37
2 Ontario            179
colSums(is.na(combined))           # any unexpected NAs after the bind?
province     year  area_ha 
       0        0        0 

Both provinces appear (BC = 37 rows, Ontario = 179 rows), and there are no unexpected NAs.

Task 5 — Reflect (the decisions the code cannot make)

  • Units: both areas are hectares — good; if one had been km² we would have had to convert first.
  • Time aggregation: BC is a yearly time series (1987–2023); Ontario is a single 2021 snapshot cross-classified by category. Stacking them treats very different time scopes as one column — a judgement call.
  • Scope: BC Harvested_ha is area harvested per year; Ontario SumOfTotalHa is total forest area by category. They are not the same quantity — the combined table is only meaningful if the reader is told this.

Task 6 — Submit

Submit the combined tibble (as a kable), a one-paragraph note on the Task 5 decisions, and all group members’ names. (Graded lab: follow Canvas.)