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.
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
- Import:
read_csv("data.csv"),readxl::read_excel(),haven::read_sav()for SPSS files. - Inspect:
glimpse(),summary(),head(). - Transform with dplyr verbs:
filter()rows,select()columns,mutate()new variables,group_by()+summarise()summaries,arrange()sorting. - Tidy with tidyr:
pivot_longer()andpivot_wider(). - Visualise with ggplot2: data + aesthetics + geometry.
- Model:
t.test(),cor.test(),lm(),glm(). - 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 returnNA. - 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.
- Keep only rows where
ageis 25 or over. - Select
id,modeandengage. - Create
engage_pct= engage / 5 × 100. - Calculate mean and standard deviation of
engagebymode. - 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.
- 01RStudio and Projects
- 02Importing and inspecting data
- 03Transforming with dplyr
- 04Visualising with ggplot2
- 05Fitting and reading lm()
Check your understanding
3 short questions. Choose an answer for each, then check. Every answer comes with an explanation.
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.
Sources and about this guide
- Wickham, H., Çetinkaya-Rundel, M., & Grolemund, G. (2023). R for data science (2nd ed.). O’Reilly. (Free online.)
- Field, A., Miles, J., & Field, Z. (2012). Discovering statistics using R. SAGE.
- 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.