Statistics & Data Analytics

R Programming for Research: Import, Transform, Visualise and Model Data

R is free, powerful and built for statistics. This guide teaches the core workflow, from loading data to a publication-quality chart and a regression model, so you can write analysis you understand and can reproduce.

  • Undergraduate · Masters · PhD
  • 6 min read
  • Practice questions
  • 2 learning resources

What is R?

R is a free, open-source language for statistical computing and graphics. Thousands of packages extend it for almost any analysis. RStudio (by Posit) is the most popular environment for using it.

Core concepts

Concept Example
Assignment x <- 5
Vector scores <- c(62, 75, 81)
Function mean(scores)
Data frame A table of variables (columns) and observations (rows)
Package install.packages("tidyverse") once, then library(tidyverse) each session
Pipe data |> filter(age > 20) |> summarise(m = mean(score))

The data analysis workflow

  1. Import: read_csv("data.csv"), readxl::read_excel(), haven::read_sav() for SPSS files.
  2. Inspect: glimpse(), summary(), head().
  3. Transform with dplyr verbs: filter() rows, select() columns, mutate() new variables, group_by() + summarise() summaries, arrange() sorting.
  4. Tidy with tidyr: pivot_longer() and pivot_wider().
  5. Visualise with ggplot2: data + aesthetics + geometry.
  6. Model: t.test(), cor.test(), lm(), glm().
  7. Communicate: Quarto or R Markdown combines code, output and text in one reproducible report.

Worked example

An illustrative dataset of fictional students.

library(tidyverse)
students <- read_csv("students.csv")

students |>
  group_by(mode) |>
  summarise(n = n(), mean_engage = mean(engage, na.rm = TRUE))

ggplot(students, aes(x = support, y = engage, colour = mode)) +
  geom_point(alpha = 0.6) +
  geom_smooth(method = "lm", se = TRUE) +
  labs(x = "Perceived support (1–5)", y = "Engagement (1–5)", colour = "Study mode")

model <- lm(engage ~ support + mode, data = students)
summary(model)

Reading the model: summary(model) shows each coefficient, its standard error, t value and p-value, plus R-squared. A support coefficient of 0.41 means each one-point increase in support is associated with 0.41 points more engagement, holding study mode constant. Check assumptions with plot(model) (residuals vs fitted, Q-Q plot).

Common mistakes

  • Forgetting na.rm = TRUE, so summaries return NA.
  • Not using a Project, leading to broken file paths.
  • Copying code without understanding it. Run it line by line and inspect the result.
  • Not checking variable types (numbers imported as text, categories not as factors).
  • Uncommented scripts. Explain why, not just what.

Explore learning resources

Worked examples, worksheets and checklists to use alongside this guide. They are learning tools, not answers to an assessment: adapt them to your own task and your institution’s requirements.

ExercisePractice script: five dplyr tasksTry these on any dataset with a grouping variable.
  1. Keep only rows where age is 25 or over.
  2. Select id, mode and engage.
  3. Create engage_pct = engage / 5 × 100.
  4. Calculate mean and standard deviation of engage by mode.
  5. Sort the result from highest to lowest mean.

Self-check: the output of task 4 should have one row per study mode. If it has one row in total, you forgot group_by().

Referenceggplot2 cheat sheet in wordsChoose the right geometry for your data.
Goal Geometry
Distribution of one numeric variable geom_histogram(), geom_density()
Compare a numeric variable across groups geom_boxplot(), geom_violin()
Relationship between two numeric variables geom_point() + geom_smooth()
Counts of categories geom_bar()
Change over time geom_line()

Always label axes with units and avoid colour as the only way to tell groups apart.

Watch the topic explained

Video lesson · about 10 minutes · in production

R and RStudio for Research

An original lesson that teaches R programming with diagrams and a worked example, with captions and a transcript. These are the chapters it will cover; each links to the matching part of this guide.

  1. 01RStudio and Projects
  2. 02Importing and inspecting data
  3. 03Transforming with dplyr
  4. 04Visualising with ggplot2
  5. 05Fitting and reading lm()
Get notified on YouTube

Check your understanding

3 short questions. Choose an answer for each, then check. Every answer comes with an explanation.

1. Which dplyr verb creates a new variable?
Show the answer

Answer: mutate(). mutate() adds or changes columns.

2. mean(x) returns NA. The most likely reason is…
Show the answer

Answer: x contains missing values and na.rm = TRUE was not set. Missing values propagate unless removed with na.rm = TRUE.

3. In ggplot2, aes() is used to…
Show the answer

Answer: Map variables to visual properties such as x, y and colour. Aesthetics map data to what you see.

A learning activity to check your own understanding. It is not an assessment and nothing is recorded.

Take your learning further

Learn R programming with a specialist Academic Expert

Sometimes a concept needs more than a written explanation. A live 1:1 session with an Academic Expert who works in R programming lets you ask your own questions, work through examples at your pace and get feedback on your own thinking.

What you can work on together

  • Set up RStudio, Projects and packages
  • Import and clean messy datasets
  • Transform data with dplyr and tidyr
  • Build clear charts with ggplot2
  • Fit and interpret statistical models
  • Debug your own code
  • Make your analysis reproducible with Quarto

How sessions help

  • Make sense of your brief and marking criteria, so you know exactly what is being asked.
  • Ask the questions you would not ask in a big class, as often as you need.
  • Get timely, specific feedback on work you have written yourself.
  • Practise explaining your reasoning out loud, ready for vivas, presentations and practicals.
  • Extra support with academic English and conventions if English is not your first language.

Teach → coach → review. Experts explain, demonstrate and give feedback. You stay the author of your work and make the decisions in it.

Frequently asked questions

Do I need to know programming to learn R?

No. Many researchers learn R as their first language. Start with the data workflow above and build gradually.

Base R or tidyverse?

Both are useful. The tidyverse is consistent and readable for data work; base R is worth knowing for many statistical functions.

Our academic integrity commitment

UniLearno supports learning, understanding and independent academic development. Our Academic Experts explain concepts, demonstrate methods, guide research and give feedback on work you have created. We do not complete assessed work, fabricate data or references, or produce anything for you to submit. You remain responsible for your own ideas, analysis, conclusions and final submission, and for following your institution’s academic integrity and AI-use rules.

Why a shortcut costs more than it saves
  • It is more visible than it looks. Markers notice when work does not match what they know of you; document history, reference lists and follow-up conversations about your work are all used to check authorship.
  • It creates a lasting risk. Services that sell completed assignments have threatened students afterwards, demanding money not to tell their university.
  • The gap does not go away. Later units, supervised exams, vivas, placements and the job itself all assume you have the skill.
  • Help that teaches is the safer route. Understanding the brief, getting feedback early and practising with an expert closes the gap for good.

Read our full integrity and responsible AI approach →

Sources and about this guide

  1. Wickham, H., Çetinkaya-Rundel, M., & Grolemund, G. (2023). R for data science (2nd ed.). O’Reilly. (Free online.)
  2. Field, A., Miles, J., & Field, Z. (2012). Discovering statistics using R. SAGE.
  3. R Core Team. R: A language and environment for statistical computing. r-project.org.
Written by
UniLearno Learning Team
Last reviewed
Found an error?
Tell us and we will check and correct it.

What do you want to develop next?

  1. Tell us what you're working on.
  2. Find the right expertise.
  3. Start learning.
Find my academic expert