Data Analysis⏱ Ongoing — integrate into your existing coding workflow; 1–2 days to establish habits

Using AI to Write and Debug Research Code

How to use AI coding assistants to write data processing scripts, debug error messages, translate analyses between languages, and document code — with guidance on verifying AI-generated code before using results in a paper.

AudienceResearchers who write code for data processing or analysis but are not software engineers — particularly those working in Python or R who spend significant time on boilerplate, debugging, or translating analytical intentions into working scripts
Tools coveredClaude, GitHub Copilot
Published July 2026

What this tutorial covers

AI coding assistants have become genuinely useful for researchers who write code but aren’t professional developers. This tutorial covers practical workflows for: generating data processing scripts from a description, debugging errors, translating analyses from one language to another (R to Python or vice versa), and documenting code so it’s reproducible. It also covers the most important thing: how to verify that AI-generated code actually does what you think it does before you report results.


Which tool to use

GitHub Copilot (in VS Code, JetBrains, or other IDEs): best for inline code completion as you type — it sees your existing code and suggests continuations, function implementations, and docstrings in context.

Claude or ChatGPT in a browser: best for conversational code generation and debugging — describe what you want, paste an error message, or ask it to explain what a piece of code does. Useful when you don’t yet know what the code should look like.

Many researchers use both: Copilot for active coding sessions, an AI chat interface for larger design questions or debugging sessions.


Task 1: Writing a data processing script from a description

Describe your data format and what you want the script to do before asking for code:

Prompt template:

“I have a CSV file with columns: [list columns and their types]. I want to: (1) [first operation], (2) [second operation], (3) [third operation]. The output should be [describe output]. Write Python/R code to do this. Add a comment on each non-obvious step explaining what it does.”

After receiving the code:

  1. Read the code before running it — does the logic match your description?
  2. Run it on a small sample first (e.g., first 100 rows) to check output shape and content
  3. Spot-check several rows manually to confirm the transformation is correct
  4. Only then run on the full dataset

Common issue: AI-generated data processing code often handles missing values in a specific way (usually dropping them) without flagging it. Always check how NaN/NA values are handled.


Task 2: Debugging error messages

Paste the complete error message and the relevant code:

Prompt template:

“I’m getting this error: [paste full error traceback]. Here is the code that produced it: [paste code]. Explain what the error means and suggest the most likely fix. If there are multiple possible causes, list them in order of likelihood.”

Tips:

  • Always paste the full traceback, not just the last line — the relevant error is often in the middle
  • Include the relevant code block, not just the line that throws the error
  • If the fix doesn’t work, tell the AI what happened: “That didn’t fix it — here’s the new error:” and iterate

AI is particularly good at: KeyError, AttributeError, type mismatches, indexing errors, and merge/join issues. It’s less reliable for: logic errors where the code runs without error but produces wrong results.


Task 3: Translating analyses between languages

If your collaborators use R and you work in Python (or vice versa):

Prompt template:

“Translate this R code to Python (using pandas and numpy where appropriate): [paste code]. Keep the logic identical. Note any differences in how the two languages handle edge cases for these operations.”

Critical step: Run both versions on the same dataset and compare outputs numerically. Small discrepancies are common — differences in default arguments (e.g., how cor() in R handles NA vs. pandas’ .corr()), different default significance levels, or different conventions for one-tailed vs. two-tailed tests.

Never assume a translation is correct without verification on real data.


Task 4: Explaining code you didn’t write

For legacy code, lab scripts, or code inherited from a previous student:

Prompt template:

“Explain what this code does, step by step. For each section, describe: (1) what it does, (2) what the input is, (3) what the output is. Flag anything that looks unusual or potentially incorrect.”

Use this to build your mental model before modifying the code. The “flag anything unusual” instruction catches patterns that might be bugs, hardcoded assumptions, or non-standard approaches the original author used.


Task 5: Writing reproducible analysis scripts

For analyses that will go into a paper, documentation matters:

Prompt template:

“Add docstrings and inline comments to this script. For each function, document: parameters, return values, and any assumptions about the input format. For non-obvious operations, add a comment explaining why this approach was chosen, not just what it does.”

Then save the annotated version as the canonical script in your project repository. When reviewers or co-authors ask how an analysis was done, the code itself answers the question.


Verifying AI-generated code before reporting results

This is the most important section of this tutorial. Code that runs without errors can still produce wrong results. Before using AI-generated code in a published analysis:

  1. Test on synthetic data with known answers. If your code computes a mean difference between groups, generate a toy dataset where you know the true difference and confirm the code recovers it.
  2. Check against a manual calculation. For a small subset, compute the result by hand (or in a spreadsheet) and compare.
  3. Validate intermediate outputs. Print or inspect data at each major transformation step — not just the final output.
  4. Replicate one key result in a different tool. If your main analysis is in Python, check one result in R or Excel. Convergence gives confidence; divergence tells you something is wrong.
  5. Have a knowledgeable person review the code. AI-generated code can contain statistically valid but scientifically inappropriate choices — the right statistical test for your study design is a domain judgment, not a coding problem.

What AI coding assistants can’t do

  • Design your analysis. Choosing the right statistical model for your research question requires understanding your data, your assumptions, and your field’s conventions — AI can implement an analysis but cannot tell you if it’s the right one to run.
  • Catch logical errors. If you describe the wrong transformation and the code correctly implements your wrong description, the output will be wrong and the code will run fine.
  • Access your data. When debugging, describe the data structure precisely — AI cannot see what your data actually looks like.