Correlation Exercises in R: 20 Practice Problems
Twenty practice problems on correlation in R: Pearson, Spearman, Kendall, test, partial, visualize. Hidden solutions.
RRun this once before any exercise
library(dplyr)
library(ggplot2)
library(ggcorrplot)
library(ppcor)
library(psych)
library(tibble)
library(tidyr)
Exercise 1: Pearson correlation
Difficulty: Beginner.
Show solution
RInteractive R
cor(mtcars$wt, mtcars$mpg)
Exercise 2: Correlation matrix
Difficulty: Beginner.
Show solution
RInteractive R
cor(mtcars)
Exercise 3: Cor matrix selected columns
Difficulty: Beginner.
Show solution
RInteractive R
cor(mtcars[, c("mpg","wt","hp","disp")])
Exercise 4: cor.test (Pearson)
Difficulty: Intermediate.
Show solution
RInteractive R
cor.test(mtcars$wt, mtcars$mpg)
Exercise 5: Spearman correlation
Difficulty: Intermediate.
Show solution
RInteractive R
cor(mtcars$wt, mtcars$mpg, method = "spearman")
Exercise 6: Kendall correlation
Difficulty: Intermediate.
Show solution
RInteractive R
cor(mtcars$wt, mtcars$mpg, method = "kendall")
Exercise 7: Handle NAs with use=
Difficulty: Intermediate.
Show solution
RInteractive R
cor(airquality$Ozone, airquality$Temp, use = "complete.obs")
Exercise 8: Pairwise NA handling
Difficulty: Advanced.
Show solution
RInteractive R
cor(airquality, use = "pairwise.complete.obs")
Exercise 9: Extract p-value from cor.test
Difficulty: Beginner.
Show solution
RInteractive R
cor.test(mtcars$wt, mtcars$mpg)$p.value
Exercise 10: 99% CI
Difficulty: Intermediate.
Show solution
RInteractive R
cor.test(mtcars$wt, mtcars$mpg, conf.level = 0.99)$conf.int
Exercise 11: Correlation heatmap
Difficulty: Intermediate.
Show solution
RInteractive R
m <- cor(mtcars)
m_long <- as.data.frame(m) |>
tibble::rownames_to_column("v1") |>
tidyr::pivot_longer(-v1, names_to = "v2", values_to = "cor")
ggplot(m_long, aes(v1, v2, fill = cor)) + geom_tile() +
scale_fill_gradient2(low = "blue", mid = "white", high = "red")
Exercise 12: Cor with significance flags
Difficulty: Advanced.
Show solution
RInteractive R
# Use psych::corr.test or Hmisc::rcorr
psych::corr.test(mtcars[, 1:4])
Exercise 13: Detect top correlated pairs
Difficulty: Advanced.
Show solution
RInteractive R
m <- cor(mtcars)
m[lower.tri(m, diag = TRUE)] <- NA
which(abs(m) > 0.8, arr.ind = TRUE)
Exercise 14: Partial correlation
Difficulty: Advanced.
Show solution
RInteractive R
ppcor::pcor.test(mtcars$mpg, mtcars$wt, mtcars$hp)
Exercise 15: Per-group correlation
Difficulty: Advanced.
Show solution
RInteractive R
iris |> group_by(Species) |>
summarise(r = cor(Sepal.Length, Petal.Length))
Exercise 16: Correlation with significance asterisks
Difficulty: Advanced.
Show solution
RInteractive R
m <- Hmisc::rcorr(as.matrix(mtcars))
m$r # estimates
m$P # p-values
Exercise 17: Visualize with pairs()
Difficulty: Beginner.
Show solution
RInteractive R
pairs(mtcars[, c("mpg","wt","hp","disp")])
Exercise 18: ggcorrplot
Difficulty: Intermediate.
Show solution
RInteractive R
ggcorrplot::ggcorrplot(cor(mtcars))
Exercise 19: Rank correlation when nonlinear
Difficulty: Advanced.
Show solution
RInteractive R
# Spearman captures monotonic non-linear better than Pearson
x <- 1:50; y <- x^2
list(pearson = cor(x, y), spearman = cor(x, y, method = "spearman"))
Exercise 20: Bootstrap CI for correlation
Difficulty: Advanced.
Show solution
RInteractive R
set.seed(1)
r_boot <- replicate(1000, {
i <- sample(seq_len(nrow(mtcars)), replace = TRUE)
cor(mtcars$wt[i], mtcars$mpg[i])
})
quantile(r_boot, c(0.025, 0.975))
What to do next
- Hypothesis-Testing-Exercises (shipped), broader inference drills.
- Linear-Regression-Exercises (shipped), modeling on correlated predictors.