Introduction: Why R for Your Thesis?
R is a free, open-source programming language for statistical computing and data visualisation. It has become increasingly popular at Canadian universities, particularly in statistics, data science, biostatistics, economics, ecology, and psychology departments. Many Canadian graduate programmes now teach R as the primary statistical tool.
This guide introduces R for thesis-level statistical analysis, with practical examples relevant to Canadian graduate students.
Getting Started With R
Installation
1. R: Download from CRAN (The Comprehensive R Archive Network) — cran.r-project.org
2. RStudio: Download from posit.co — the most popular IDE for R
3. Both are free and available for Windows, Mac, and Linux
RStudio Interface
- Console: Where R code is executed
- Script editor: Where you write and save code
- Environment: Shows your loaded data and objects
- Plots/Help/Files: Viewing outputs and documentation
Essential Packages for Thesis Research
Install these packages to get started:
install.packages(c("tidyverse", "psych", "car", "lme4", "ggplot2", "knitr", "rmarkdown", "haven", "readxl"))
Key packages:
- tidyverse: Data manipulation and visualisation (dplyr, ggplot2, tidyr)
- psych: Descriptive statistics, reliability analysis, factor analysis
- car: ANOVA, regression diagnostics
- lme4: Mixed-effects/multilevel models
- haven: Import SPSS, SAS, and Stata files
- rmarkdown/knitr: Create reproducible reports
Data Import and Wrangling
Reading Data
R can import data from virtually any format:
library(readr)
data <- read_csv("my_survey_data.csv")
library(haven)
spss_data <- read_sav("survey.sav")
library(readxl)
excel_data <- read_excel("data.xlsx", sheet = 1)
Canadian Data Sources in R
Several R packages provide direct access to Canadian data:
- cansim: Access Statistics Canada data tables directly
- cancensus: Canadian Census data via the CensusMapper API
- rcanvec: Canadian vector data
- weathercan: Canadian weather data from Environment Canada
library(cansim)
unemployment <- get_cansim("14-10-0287-01")
Data Manipulation With dplyr
The dplyr package provides intuitive data manipulation:
library(dplyr)
clean_data <- raw_data %>%
filter(!is.na(satisfaction_score)) %>%
mutate(
age_group = case_when(
age < 25 ~ "Under 25",
age < 35 ~ "25-34",
age < 45 ~ "35-44",
TRUE ~ "45+"
),
province = factor(province)
) %>%
select(id, age_group, province, satisfaction_score, income)
Common Statistical Analyses
Descriptive Statistics
library(psych)
describe(data$satisfaction_score)
summary_by_group <- data %>%
group_by(province) %>%
summarise(
n = n(),
mean = mean(satisfaction, na.rm = TRUE),
sd = sd(satisfaction, na.rm = TRUE),
median = median(satisfaction, na.rm = TRUE)
)
t-Tests
t.test(score ~ group, data = my_data)
t.test(data$pre_score, data$post_score, paired = TRUE)
ANOVA
model <- aov(score ~ group, data = my_data)
summary(model)
TukeyHSD(model)
library(car)
Anova(model, type = "III")
Correlation
cor.test(data$x, data$y, method = "pearson")
library(psych)
corr.test(data[, c("var1", "var2", "var3")])
Regression
model <- lm(outcome ~ predictor1 + predictor2 + predictor3, data = my_data)
summary(model)
confint(model)
library(car)
vif(model)
Chi-Square Test
table <- table(data$gender, data$preference)
chisq.test(table)
Mixed-Effects Models
library(lme4)
model <- lmer(score ~ treatment + time + (1|participant), data = my_data)
summary(model)
Data Visualisation With ggplot2
R excels at creating publication-ready visualisations:
Basic Plots
library(ggplot2)
ggplot(data, aes(x = province, y = score, fill = province)) +
geom_boxplot() +
labs(title = "Satisfaction Scores by Province",
x = "Province", y = "Satisfaction Score") +
theme_minimal() +
theme(legend.position = "none")
Customised Thesis Plots
ggplot(data, aes(x = income, y = satisfaction)) +
geom_point(alpha = 0.6, colour = "#003366") +
geom_smooth(method = "lm", colour = "#C41E3A", se = TRUE) +
labs(title = "Relationship Between Income and Satisfaction",
subtitle = "Canadian Graduate Students (n = 450)",
x = "Annual Income (CAD)",
y = "Life Satisfaction Score") +
scale_x_continuous(labels = scales::dollar_format(prefix = "$")) +
theme_classic(base_size = 12)
Saving Plots
ggsave("figures/satisfaction_by_province.png", width = 8, height = 6, dpi = 300)
Reproducible Research With R Markdown
R Markdown allows you to integrate code, output, and narrative in a single document:
Benefits for Thesis Writing
- Reproducibility: Anyone can re-run your analysis
- Transparency: Code and results are linked
- Efficiency: Update analyses and documents simultaneously
- Multiple output formats: PDF, Word, HTML
Thesis Chapters in R Markdown
Some Canadian universities accept theses written entirely in R Markdown:
- bookdown package creates multi-chapter documents
- thesisdown provides thesis templates for several institutions
- Check if your university's graduate studies office accepts R Markdown output
Tips for Thesis Success
1. Use R Projects
Create an R Project (.Rproj) for your thesis to keep all files organised and ensure reproducibility.
2. Script Everything
Never rely on point-and-click operations. Write scripts for every analysis so you (and your supervisor) can reproduce and verify your work.
3. Comment Your Code
Write clear comments explaining what each section of code does and why. Your future self will thank you.
4. Version Control
Use Git and GitHub to track changes to your analysis scripts. Many Canadian universities provide free private GitHub repositories for students.
5. Seek Help
- University R user groups (many Canadian campuses have them)
- Stack Overflow (tag with [r])
- RStudio Community
- Your university's statistical consulting service
Conclusion
R is a powerful, free, and increasingly essential tool for graduate research at Canadian universities. By learning to import, manipulate, analyse, and visualise data in R, you gain skills that will serve you throughout your academic and professional career.
---
Need help with R programming for your thesis? Our Canadian statisticians and data analysts can assist with code development, analysis, and visualisation. Contact us for expert support.
Dr. James Park
Dr. James Park holds a PhD in Statistics from the University of Waterloo and has consulted on quantitative methodology for over 200 graduate theses across Canadian institutions.