Agentic Data Analysis

A Cleaning Workshop

Last Time

  1. An agent calls tools in a loop.
  2. A mode sets which calls run.
  3. A trace backs up each number.
  4. Check a plot reading with numbers.

Key Ideas for Today

  1. A cleaning decision depends on the use.
  2. Audit by hand before you delegate.
  3. In Cleaning mode, you make the calls.
  4. A property checks any cleaning.

While you’re waiting…

In a new folder outside your Project 6 repo:

  1. Copy the ZEV workbook from Project 2 (or Project 6’s run-1/data/) into data/.
  2. Open the folder in Positron and start a script:
library(tidyverse)
library(readxl)
zev_raw <- read_excel("data/New_ZEV_Sales_Last_updated_07-17-2026_ada.xlsx",
                      sheet = "County")

The Workshop

Today’s plan

1. By hand

Audit one column with dplyr. Decide.

2. With an agent

Same raw table, Cleaning mode. Answer its questions.

3. Check both

One property, two scripts. What would it miss?

Work in pairs. One laptop runs R; the other keeps notes.

Round 1: By Hand

Audit "Out Of State"

distinct(COUNTY) gives 59 values. California has 58 counties.

With your partner, write dplyr code to answer:

  1. How many rows have COUNTY == "Out Of State"? How many vehicles do they represent?
  2. The same two numbers for 2026 alone.
04:00

What we found

zev_raw |>
  filter(COUNTY == "Out Of State") |>
  summarize(rows = n(), vehicles = sum(`Number of Vehicles`))
# A tibble: 1 × 2
   rows vehicles
  <int>    <dbl>
1  1752    32838
zev_raw |>
  filter(COUNTY == "Out Of State", `Data Year` == 2026) |>
  summarize(rows = n(), vehicles = sum(`Number of Vehicles`))
# A tibble: 1 × 2
   rows vehicles
  <int>    <dbl>
1   125     1031

What does "Out Of State" mean?

Vehicles are assigned to a county by the owner’s mailing address.

A vehicle registered in California whose owner’s mailing address is in another state.

The Data Cleaning slide 'Decision: Out Of State', with a vote on what to do with those rows: A. Drop them, B. Keep them as they are, C. Keep them, but add a column that flags them.

Vote again:

Use 1: How many new ZEVs were sold in California in the first half of 2026?

Use 2: A map of new ZEVs by county in 2026.

Our cleaning

zev <- zev_raw |>
  rename(year = `Data Year`, quarter = Quarter, county = COUNTY,
         fuel_type = FUEL_TYPE, make = MAKE, model = MODEL,
         n_vehicles = `Number of Vehicles`) |>
  # Keep out-of-state registrations, flagged: they count in a statewide
  # total but have no county to map
  mutate(in_state = county != "Out Of State")
zev |>
  filter(year == 2026) |>
  summarize(vehicles = sum(n_vehicles))
# A tibble: 1 × 1
  vehicles
     <dbl>
1   151525
zev |>
  filter(year == 2026, in_state) |>
  group_by(county) |>
  summarize(vehicles = sum(n_vehicles)) |>
  nrow()
[1] 57

Round 2: With an Agent

Cleaning Mode

A Posit Assistant mode that audits a data set, then asks you about each decision before it changes anything.

Choose it from the mode picker.

Tool Who acts
read, executeCode the agent
AskUser you
write, edit the agent, once you agree

The agent proposes; the analyst decides.

Data Cleaning Mode

Clean the ZEV data.

Paste this prompt into Posit Assistant in Data Cleaning mode, run it, then hands off the keyboard.

  1. What information did the prompt include? What omit?
  2. How many questions did it surface for you?
  3. What did calls did it make?
  4. How many tokens did it transfer?
  5. What were the questions?
02:00

Data Cleaning Mode

Clean the ZEV data.

Decide which route to take on each question, then have it create the cleaning script.

In the script, find the code that handles "Out Of State".

  1. Did it drop, keep, or flag? Is that what we told it?
  2. How many rows did that step touch? Does it match our audit?
02:00

Round 3: Check Both

Property

A statement that every correct output must satisfy, whatever code produced it. A test of a property can check many different solutions at once.

Adding a county flag should not change the statewide vehicle total.

One check, two scripts

stopifnot(
  sum(zev$n_vehicles) == sum(zev_raw$`Number of Vehicles`)
)

Now run it on the agent’s clean data. Change only the names.

zev_agent <- read_csv("data/clean/[its file].csv")
stopifnot(
  sum(zev_agent$[its column]) == sum(zev_raw$`Number of Vehicles`)
)
02:00

What would it miss?

zev_wrong <- zev |>
  mutate(county = if_else(county == "Out Of State", "Los Angeles", county))

stopifnot(
  sum(zev_wrong$n_vehicles) == sum(zev_raw$`Number of Vehicles`)
)

This cleaning is wrong, and it passes. What check would catch it?

A stronger property

Cleaning should not move a vehicle from one county to another.

before <- zev_raw |>
  group_by(county = COUNTY) |>
  summarize(vehicles = sum(`Number of Vehicles`))

after <- zev_wrong |>
  group_by(county) |>
  summarize(vehicles = sum(n_vehicles))

expect_equal(after, before)
Error:
! Expected `after` to equal `before`.
Differences:
Attributes: < Component "row.names": Numeric: lengths (58, 59) differ >
Component "county": Lengths (58, 59) differ (string compare on first 58)
Component "county": 28 string mismatches
Component "vehicles": Numeric: lengths (58, 59) differ

Guess the Metaphor

Cleaning with an agent is like a bathhouse attendant: it scrubs whatever it can reach, so tell it what’s dirt and what’s worth keeping before it starts, and check the water after.

Key Ideas for Today

  1. A cleaning decision depends on the use.
  2. Audit by hand before you delegate.
  3. In Cleaning mode, you make the calls.
  4. A property checks any cleaning.

For Next Time

  1. Sign up for Quiz 7 (12/1-12/4).
  2. Project 6 Studio the week of 11/30.
  3. PS 7 due 12/5.
  4. Monday: SQL through dplyr.

Worked Example

If you missed class

Raw data: the County sheet, "Out Of State" rows

zev_raw |>
  filter(COUNTY == "Out Of State", `Data Year` == 2026) |>
  select(COUNTY, FUEL_TYPE, MAKE, `Number of Vehicles`) |>
  head(4)
# A tibble: 4 × 4
  COUNTY       FUEL_TYPE MAKE     `Number of Vehicles`
  <chr>        <chr>     <chr>                   <dbl>
1 Out Of State Electric  Audi                        1
2 Out Of State Electric  BMW                         4
3 Out Of State Electric  Cadillac                    3
4 Out Of State Electric  Cadillac                    1

Before and after, 2026 (Q1–Q2)

Vehicles
Raw, all rows 151,525
Out of state 1,031
Statewide total (Use 1) 151,525
On the county map (Use 2) 150,494

Our script is on Our cleaning; the checks are in Round 3.