Can AI Replace Learning R and Data Analysis?

Quick vote: Yes, no, or it depends? What makes you think so?

What course and year are you studying? Have you used AI at university or in everyday life, such as for cooking? What did you ask it to do, and did you check its answer?

It’s fine if you have not used AI yet. We will return to the opening question at the end.

Glowing carved pumpkins on a dark background

Today: One Analysis, One AI Workflow

By the end of 50 minutes, you will be able to:

  • ask Copilot for help with R code using a real dataset;
  • run and check code in RStudio;
  • repair an error without guessing;
  • check a statistical interpretation against the data source.

Keep the code and your own explanation. A plausible AI answer is a starting point for checking.

Start With What You Have Seen In R

You may recognise R being used to:

  • calculate with numbers and load a dataset;
  • get averages and other descriptions with psyntur;
  • make plots with psyntur;
  • keep, remove, or create columns and rows with dplyr (tidyverse).

You do not need to remember any function names. We will read the code together, ask Copilot to explain it, and check what happens when we run it.

You do not need a new statistical test or a new R package for this workshop.

Use Your NTU Copilot Account

  1. Open these slides on your own device: rpubs.com/jensroes/genAI-rstudio-2026.
  2. Sign in to Microsoft 365 with your NTU account and open Copilot.
  3. In RStudio, choose File → New File → R Script. Copy the short R examples from these slides as we reach them.

NTU identifies Microsoft Copilot as its preferred GenAI tool for students. Use only the supplied public candy data in today’s prompts. Keep participant or other private data out of the chat.

Copilot can suggest code. RStudio runs it, and you decide whether the result makes sense.

A Psychology Question With Halloween Data

Illustration of a carved Halloween pumpkin

Question: In these online candy choices, were chocolate sweets preferred more often than other sweets?

These are real, observed choices, not invented example data. The FiveThirtyEight candy ranking dataset records 85 sweets and results from approximately 269,000 online pairwise matchups. The next slide links to the original published CSV.

  • chocolate: 1 if the sweet contains chocolate; 0 otherwise.
  • winpercent: the percentage of its matchups that the sweet won.
  • sugarpercent: its sugar percentile within this dataset, not the percentage of its mass that is sugar.

One row is one sweet. The data show what happened in these online choices; they do not represent everyone’s preferences.

Load The Data From Its Source

The data are publicly available as a CSV file from FiveThirtyEight. Copy this code into your new R script and run it:

# Make data tools available.
library(tidyverse)

# Make the course's plot and summary tools available.
library(psyntur)

# Join the two parts of the data file's web address.
candy_url <- paste0(
  "https://raw.githubusercontent.com/fivethirtyeight/data/",
  "master/candy-power-ranking/candy-data.csv"
)

# Load the data and name it candy_data.
candy_data <- read_csv(candy_url)

Look At The Data

names(candy_data)
nrow(candy_data)
glimpse(candy_data)

names() shows column names; nrow() counts rows (sweets); glimpse() previews the columns and their values.

Run these lines in your script. Find chocolate and winpercent in the output before asking Copilot for code.

A Prompt That Gives Useful Context

I am learning RStudio. I have loaded psyntur and a dataset called candy_data. Each row is a sweet; winpercent is how often it won online matchups, from 0 to 100. Please show me how to make a histogram of winpercent using psyntur. Explain what each part of the code does. Do not add packages or invent column names.

A useful prompt says what you want, names the actual objects and variables, and describes any method or limits.

Try this in Copilot. Compare its code with your data before running it.

Run The Plot And Check It

Here is one working line:

histogram(data = candy_data, x = winpercent)

histogram() makes the plot. data names the dataset and x names the column to show along the horizontal axis.

  • Does RStudio produce a histogram?
  • Is the horizontal axis winpercent?
  • Are the values plausible for percentages between 0 and 100?
  • Can you explain what one observation represents?

If Copilot plots sugarpercent, R still draws a histogram—but it shows sugar ranking. Our preference question needs winpercent on the horizontal axis.

The Plot We Expect

The bars count sweets whose win percentages fall in each interval. They do not count voters.

Debug A Realistic Error

Run this line in RStudio:

histogram(data = candy_data, x = win_percent)

The useful part of R’s error is: object 'win_percent' not found. Your Console may show more detail.

Run names(candy_data) to see the available columns. What is different? Then ask Copilot:

My histogram code gives object 'win_percent' not found. Here is my code and the output of names(candy_data). What is the smallest correction? Explain how you checked it.

Paste your code and column names into the prompt. Change one thing, then rerun the line.

Check The Explanation Too

Consider this AI claim:

“A candy with sugarpercent = 0.80 is 80% sugar.”

The dataset description says sugarpercent is a percentile among these sweets. The claim confuses a ranking with a physical amount.

Ask Copilot to explain sugarpercent. Then check the answer against the source. How would you correct it in one sentence?

Check unfamiliar variables against the source before interpreting a result.

Prepare The Data

Keep the columns we need and remove any sweets with a missing winpercent:

candy_selected <- select(candy_data, competitorname,
                         chocolate, winpercent)
candy_ready <- drop_na(candy_selected, winpercent)

nrow(candy_selected)
nrow(candy_ready)

candy_selected has the three chosen columns. candy_ready also removes any rows with a missing winpercent. nrow() lets us compare the row counts.

Ask Copilot to explain what each line does. Check the number of rows before and after drop_na(); did any sweets disappear?

Describe The Two Groups

Ask Copilot: “How can I use psyntur to find the average winpercent separately for sweets with chocolate = 0 and chocolate = 1?” Check its suggestion against this example:

describe(
  data = candy_ready,
  by = chocolate,
  mean_win = mean(winpercent)
)

describe() calculates a result. data supplies the dataset; by creates two groups; mean() finds each group’s average. mean_win is the name we give the result.

Run it in RStudio. Check that there are two groups and that the means are percentages from 0 to 100.

What Did R Actually Find?

# A tibble: 2 × 2
  chocolate mean_win
      <dbl>    <dbl>
1         0     42.1
2         1     60.9

The chocolate sweets in this dataset have a higher mean win percentage. Ask Copilot for a one-sentence interpretation, then check:

  • Did it give the correct direction and units?
  • Did it describe sweets and online matchups accurately?
  • Did it claim that chocolate caused the difference? These data do not show that.

Describe the observed pattern. Do not turn an association into a causal claim.

If You Finish Early: Change The Data

Ask Copilot to explain this code, then predict and check the result in RStudio:

chocolate_sweets <- filter(candy_ready, chocolate == 1)
chocolate_sweets <- mutate(chocolate_sweets,
                           win_fraction = winpercent / 100)

filter() keeps chocolate sweets; mutate() creates a new column with each win percentage divided by 100.

  • How many rows should chocolate_sweets have?
  • What does win_fraction mean for a sweet with winpercent = 60?
  • Does converting a percentage to a fraction change the underlying result?

Your Own Explanation

Complete this sentence as a comment in your own R script:

“Among the sweets in this dataset, the mean win percentage was ___ for chocolate sweets and ___ for other sweets. This describes online choices; it does not show that adding chocolate causes a sweet to win more often.”

Then note one thing Copilot got wrong or left unclear and how you checked it.

Talk through your sentence with a partner. Keep your checked code and explanation.

The Workflow To Take Away

  1. Inspect the data and find out what each variable measures.
  2. Give Copilot the task, actual names, package, and constraints.
  3. Read the proposed code before you run it.
  4. Run it in RStudio; inspect the error, output, or plot.
  5. Check the interpretation against the data source and what you learned in class.

NTU’s academic integrity policy says AI should support your independent work and that AI use in assessments should be declared on the assessment cover sheet.

You are responsible for the code and conclusions you submit.

Questions

Has today’s work changed your answer to our opening question? Which analysis skills helped you check Copilot’s code and explanation?

AI can suggest code. You still need to understand the question, data, code, and result.