Guided Case Study: From Excel to R

ImportantPractice demo only

This activity is a guided practice lab in the OER book. It is designed to help you learn the workflow before completing the official lab assignment on Canvas. The Canvas lab is the graded assignment. Use this book activity to practice, compare your work with the reference solution, and learn how to write an AI verification prompt.

NoteData for this chapter

This chapter uses the BC silviculture workbook (bc_disturbance_reforestation.xlsx) — the same file you used in the Excel chapters. Keep it in your data/ folder. For the source and details, see the Datasets Used in This Book page.

What this chapter is

This is your first, guided case study — an early bridge from Excel (Chapters 1–3) to R (Chapters 4–5). You will answer one small forestry question twice: once with the Excel skills you already have, and once with the first R tools. Seeing the same answer on both sides is the point.

It deliberately uses only the skills taught so far: Excel inspection, cleaning, formulas, PivotTables, and charting, plus early R setup, import, inspection, basic functions, the native pipe |>, and Quarto. It does not need joins, reshaping, grouped summarise(), or ggplot2 — those come later, and the full independent case study at the end of the course (Chapter Integrated Case Study) uses them all.

Chapter goals

By the end you will be able to:

  • restate a small forestry question and decide what you need to answer it;
  • answer it in Excel (inspect → formula / PivotTable → chart);
  • answer the same question in early R (import → inspect → a basic calculation with the native pipe) and confirm the two agree;
  • verify a result instead of trusting it, and write an AI prompt that checks your work rather than doing it for you.

The question

“What was the average annual reforested area in British Columbia over the whole record, and which single year was the highest?”

Nothing here needs a join or a grouped summary — just an average, a maximum, and careful reading of the file.

Part A — answer it in Excel

You already know these moves from Chapters 1–3:

  1. Inspect. Open the Data sheet. Confirm the columns (Fiscal_Year, Reforestation_ha, …), the units (hectares), and that there are no missing values.
  2. Average. In an empty cell, =AVERAGE(H2:H38) on the Reforestation_ha column.
  3. Maximum + its year. =MAX(H2:H38) for the largest value, and =INDEX(A2:A38, MATCH(MAX(H2:H38), H2:H38, 0)) for the fiscal year it happened in.
  4. Chart (optional). A quick line chart of Reforestation_ha by Fiscal_Year shows the shape behind the numbers.

Write your three answers down — you will check them against R next.

Part B — answer it in early R

Use this after your own attempt. It should check your reasoning, not hand you the answer:

“My question is [question], and I plan to answer it with these steps: [list your Excel and early-R steps]. Check whether my plan uses the simplest tools that answer the question, whether the order makes sense, and which checks would confirm the result. Do not solve the case study for me.”

The same three answers, now reproducibly. This uses only Chapter 4–5 skills: read_excel(), inspection, base functions, and the pipe.

bc <- read_excel(here("data", "bc_disturbance_reforestation.xlsx"),
                 sheet = "Data")
dim(bc)        # rows and columns
[1] 37  8
names(bc)      # column names — confirm Reforestation_ha is here
[1] "Fiscal_Year"                   "Clearcutting_ha"              
[3] "Clearcutting_with_reserves_ha" "Partial_cutting_ha"           
[5] "Harvested_ha"                  "Natural_Disturbance_ha"       
[7] "Total_Disturbance_ha"          "Reforestation_ha"             
# Average reforested area (same as Excel =AVERAGE)
bc$Reforestation_ha |> mean()
[1] 223353.1
# The maximum, and the fiscal year it happened in
bc$Reforestation_ha |> max()
[1] 274616.1
bc$Fiscal_Year[which.max(bc$Reforestation_ha)]
[1] 2006

The pipe |> reads “take the Reforestation_ha column, and then take its mean.” No new packages, no group_by() — just the moves you already have.

The bridge: the R average should match your Excel =AVERAGE to the cent, and the R maximum and its year should match your Excel MAX / INDEX/MATCH. If they don’t, something is off in one of the two — that mismatch is a verification win, not a failure.

Practice Demo Lab

ImportantPractice demo only (not the graded Canvas lab)

Work through the six steps below. The graded version is on Canvas.

1 — Your attempt. For a different column — Harvested_ha — compute in R: (a) the average harvested area, (b) the maximum, and (c) the fiscal year of the maximum. Then confirm each against an Excel formula.

2 — Reference solution.

bc$Harvested_ha |> mean()                              # (a) average
[1] 199053.3
bc$Harvested_ha |> max()                               # (b) maximum
[1] 251557.3
bc$Fiscal_Year[which.max(bc$Harvested_ha)]             # (c) year of max
[1] 1988

(In Excel: =AVERAGE(E2:E38), =MAX(E2:E38), and =INDEX(A2:A38, MATCH(MAX(E2:E38), E2:E38, 0)).)

3 — Compare your work with the reference solution. Where did your numbers match? Where did they differ? What caused any difference (wrong column? a stray blank cell in the Excel range? the wrong sheet)? How did you fix it?

4 — Write your own AI verification prompt. Draft a prompt that asks an AI to check your reasoning and outputs — not to hand you the answer. Include the file name, the column, what you computed, and the checks you want it to confirm.

5 — Model AI verification prompt.

“I am using R in FRST 232. I have a tibble bc from bc_disturbance_reforestation.xlsx (Data sheet), columns including Fiscal_Year and Harvested_ha. I computed the mean, the max, and the year of the max of Harvested_ha. Here is my code and output: [paste]. Please explain what each line does, tell me whether it correctly answers the question, and list checks I can run to confirm it (for example, comparing to an Excel =AVERAGE/=MAX, and checking for blank cells). Do not give me a final assignment answer — help me verify my own.”

6 — Reflection: what changed after checking? In two or three sentences: did Excel and R agree the first time? If not, what was wrong? What will you check first next time?

Done

You have answered one forestry question in two tools and verified they agree. That habit — compute, then confirm — is the foundation of every later chapter, and of the full independent case study at the end of the course.