Coding with AI

Moderate · Data & Analysis track · ~35 min hands-on + readings and quiz

← Back to the Data & Analysis track

A strong pair-programmer for R, Python, and Stata — that you always review.

What you’ll be able to do

  • Generate, explain, debug, and refactor code with AI
  • Review AI-written code critically before running it
  • Know what must never go into a code prompt

Overview

AI is a capable pair-programmer: it drafts, explains, and fixes code fast. The risk is running code you don’t understand. Read it, test it on a small case, and keep the parts that matter under your eye.

Use approved tools, and never paste sensitive data, credentials, or API keys into a prompt.

Practice activities

Activity 1 · Moderate — Explain and fix a broken script

Time ~18 min · Tools ChatGPT Edu + R (or Python)

Goal. Use AI to debug code you actually understand before trusting it.

Setup. Download the Crash Data 2020–2024 (CSV). Below are buggy scripts in R and Stata — pick your language. Each should count pedestrian-involved crashes but returns 0 (wrong column name, NAs not handled):

R version:

library(tidyverse)
crashes <- read_csv("crashes.csv")
ped <- crashes %>% filter(PEDESTRIAN > 0)   # column may be named differently; NAs ignored
nrow(ped)

Stata version:

import delimited "crashes.csv", clear
count if pedestrian > 0   // variable name may differ; missings treated as large numbers

Steps.

  1. Paste the script, the column names, and the wrong result into ChatGPT Edu:

    This [R/Stata] script should count Philadelphia crashes involving a pedestrian, but it returns 0 (or the wrong number). Here are the script and the actual column names. Explain what’s wrong and show the minimal change — don’t rewrite the whole thing.

  2. Apply the fix (likely the correct field name + NA handling) and run it.

  3. Confirm the fix is actually correct:

    How do I confirm this fix is right, not just non-erroring?

Expected result. A correct pedestrian-crash count that matches the number from the Data Analysis Basics page.

Check your work. Compare against your earlier count. “No error” is not the same as “correct.”

Common pitfalls. Never paste credentials or non-public data into a prompt. Always verify the output number, not just that the code ran.

Stretch (optional). Ask AI to add a guard that warns if the pedestrian field contains unexpected values.

Activity 2 · Moderate — Refactor with a sanity check

Time ~15 min · Tools ChatGPT Edu + R or Python

Goal. Improve working code and add a test that would actually catch a mistake.

Steps.

  1. Refactor:

    Refactor this into a function crashes_by_year(df, count_field) that returns a tidy yearly count, with input checks. Also write a small sanity-check test using a tiny fake data frame with known values.

  2. Run the test and confirm it passes.

  3. Note exactly what changed and why you accepted it.

Expected result. A reusable function plus a passing test.

Check your work. Run the function on the real crash data — totals should match prior results. Then break the function on purpose; the test should fail. If it still passes, the test is too weak.

Common pitfalls. A test that can’t fail is useless. Read the refactored code; don’t run what you don’t understand.

Stretch (optional). Ask for the same function in the other language (R↔︎Python) and confirm both return identical counts.

Activity 3 · Moderate — Let the agent close the loop: Claude Code debugs the crash script

Time ~15 min · Tools Claude Code

Goal. See what changes when the AI can actually run your code — Claude Code executes the buggy script, reads the real error and real column names itself, fixes it, and re-runs to confirm, with you reviewing every change before it’s applied.

Setup. Make a folder (e.g., crash-debug/) containing two files: the buggy script from Activity 1 saved as count_ped.R, and the Crash Data 2020–2024 CSV saved as crashes.csv. Open a terminal in that folder and start Claude Code (claude). You’ll need R installed locally, plus a Claude Code license — it’s a consumer/PI-purchased tool, not Drexel-supported.

Steps.

  1. Instead of pasting the script into a chat, point the agent at the real files and let it hit the real failure:

    Run count_ped.R in this folder. It should count pedestrian-involved crashes in crashes.csv but returns 0, which is wrong. Look at the actual column names in the CSV, figure out why the filter matches nothing, and propose a minimal fix. Show me the diff before you change anything.

    Watch what it does: it runs the script, sees the 0 (or an “object not found” error) itself, inspects the CSV header itself, and explains the mismatch — no copy-paste round-trip. Review the proposed diff and approve it only if you understand it.

  2. Don’t take “it ran” as “it’s right.” Make the agent spot-check its own fix against the raw data:

    Now verify the fix independently: print the actual column names from crashes.csv, show the distinct values in the pedestrian column including NAs, and confirm the new count is consistent with a manual tally of that column.

    The point is the same as Activity 1’s follow-up question — evidence the fix is correct, not just non-erroring — except here the agent gathers the evidence by running code, and you check its reasoning.

  3. Add the same guard as Activity 1’s stretch goal, and make the agent prove it works:

    Add a check that warns if the pedestrian column contains unexpected values (negative numbers, non-numeric entries). Then create a small copy of the CSV with one deliberately bad row and run the script against it to confirm the warning actually triggers.

    A guard you’ve never seen fire is a guard you’re trusting on faith — this closes that loop too.

Expected result. A corrected script producing the same pedestrian-crash count you got on the Data Analysis Basics page, with the agent having verified the column names and count against the raw CSV itself, plus a working warning that you watched trigger on a bad row.

Check your work. Still compare the final count against your Data Analysis Basics number yourself. The agent verified its own work — that’s better than nothing, but it’s the same system checking itself. Your independent number is the ground truth here, and “it ran without error” is still not “it’s correct.”

Common pitfalls. Never give credentials, API keys, or tokens to any AI tool — and that goes double for an agent that can execute code and read files on your machine; keep them out of the folder you point it at. Claude Code is a consumer/PI-purchased tool, not Drexel-supported: Low Risk Data only (this public crash CSV qualifies) unless you have separate approval. Also resist auto-approving every action — reviewing each diff and command is the whole exercise.

Stretch (optional). Ask Claude Code to write a tiny test file (e.g., using testthat) that locks in the correct count and the bad-value warning, so future edits to the script get checked automatically.

Check your readiness

Answer these, then check — your score suggests whether to dive in or skim the readings first.

Useful resources