Chapter 52: R Exercises
R Exercises” — which is perfect timing.
Because reading and listening is not enough — real learning happens when you type code yourself, make mistakes, fix them, get frustrated for 30 seconds, then feel that little victory when it works.
So today I’m switching hats: instead of explaining one concept, I’m going to give you a structured set of exercises — from very easy to medium difficulty — exactly like a patient teacher would hand out in a classroom.
Each exercise has:
- Clear task
- Small data / starter code (so you don’t waste time typing data)
- Expected output / hint what you should see
- Optional “stretch” challenge for extra practice
Instructions for you
- Open RStudio (local or Posit Cloud or rdrr.io/snippets)
- Create a new script
- Copy-paste each exercise one by one
- Type the solution yourself — don’t just read my answer
- Run it → compare with expected result
- If stuck → scroll down to the solution (but try first!)
Let’s start — we’ll do 10 practical exercises covering everything we learned so far.
Exercise 1 – Very Easy Warm-up (Variables + Printing)
Task Create three variables:
- city = “Hyderabad”
- temp = 29.5
- is_summer = TRUE
Then print a nice sentence using cat() or paste(): “Today in Hyderabad the temperature is 29.5 °C and it feels like summer: TRUE”
Stretch Add a fourth variable humidity = 62 and include it in the sentence.
Exercise 2 – Vectors & Basic Math
Task Create a vector called marks with these values: 78, 92, 65, 88, 71, 84, 95, 59
- Calculate mean, median, min, max
- How many students scored above 80? (use sum() on logical vector)
- Create a new vector adjusted_marks = marks + 5
Expected output mean = 79 median = 81 min = 59 max = 95 Students above 80: 4
Exercise 3 – Data Frame Creation
Task Create a data frame students with 5 rows:
Columns:
- name: “Aarav”, “Priya”, “Rahul”, “Sneha”, “Vikram”
- age: 22, 21, 24, 23, 20
- marks: 92, 85, 68, 78, 88
- city: “Hyd”, “Bng”, “Del”, “Mum”, “Hyd”
- Print the data frame
- Add a column passed = marks >= 70
- Show only students from “Hyd”
Stretch Add column grade: “A” if marks >= 85, “B” otherwise (use ifelse)
Exercise 4 – Filtering & Summarising
Task Using the students data frame from exercise 3:
- Filter students with marks > 80
- Calculate average marks of students from “Hyd”
- Count how many passed
Expected Average Hyd marks: 90 Passed: 4
Exercise 5 – Simple Plot (Base R)
Task Using marks vector from exercise 2:
- Create a scatter plot of marks vs position (1 to 8)
- Add title “Student Marks”
- Color points red if marks < 70, blue otherwise
Stretch Add horizontal line at mean marks (use abline)
Exercise 6 – ggplot2 Scatter
Task Using built-in iris data:
|
0 1 2 3 4 5 6 |
data(iris) |
Create a ggplot scatter: x = Sepal.Length, y = Petal.Length color = Species Add smooth line (method = “loess”) Use theme_minimal()
Stretch Add facet_wrap(~ Species)
Exercise 7 – Bar Chart (ggplot2)
Task Create this data frame:
|
0 1 2 3 4 5 6 7 8 9 |
df <- data.frame( area = c("Gachibowli", "Hitech City", "Banjara Hills", "Kukatpally"), avg_bill = c(4850, 6200, 7800, 4100) ) |
Make a bar chart: x = area (ordered by avg_bill descending) y = avg_bill fill = area Add text labels on top Use coord_flip() so names are readable
Exercise 8 – Percentiles & Outliers
Task Using mtcars$mpg:
- Find 10th, 25th, 50th, 75th, 90th percentile
- How many cars have mpg > 90th percentile?
- Flag outliers (mpg > 95th percentile or < 5th)
Exercise 9 – Mean vs Median Comparison
Task Create vector:
|
0 1 2 3 4 5 6 |
income <- c(48000, 52000, 62000, 75000, 82000, 95000, 145000, 280000, 1200000) |
- Calculate mean & median
- Explain in a comment why they are so different
- Remove the highest value → recalculate both
Exercise 10 – Mini Project (Put everything together)
Task Create this data frame:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
weather <- data.frame( day = c("Mon","Tue","Wed","Thu","Fri","Sat","Sun"), temp = c(28.5, 29.8, 30.2, 27.9, 31.4, 29.1, 32.0), humidity = c(65, 55, 72, 80, 45, 60, 68), rained = c(FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE) ) |
Do the following:
- Add column is_hot = temp >= 30
- Calculate average temp
- Find max & min temp and which day
- Count rainy days
- Plot temp over days (line + points)
- Filter days that are hot AND humid (>=60)
Stretch Make it a ggplot with color = rained
Solutions (scroll down only after trying!)
Ex 1 solution
|
0 1 2 3 4 5 6 7 8 9 10 |
city <- "Hyderabad" temp <- 29.5 is_summer <- TRUE cat("Today in", city, "the temperature is", temp, "°C and it feels like summer:", is_summer, "\n") |
Ex 2 solution
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
marks <- c(78, 92, 65, 88, 71, 84, 95, 59) mean(marks) # 79 median(marks) # 81 min(marks) # 59 max(marks) # 95 sum(marks > 80) # 4 adjusted_marks <- marks + 5 |
Ex 3 solution
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
students <- data.frame( name = c("Aarav","Priya","Rahul","Sneha","Vikram"), age = c(22,21,24,23,20), marks = c(92,85,68,78,88), city = c("Hyd","Bng","Del","Mum","Hyd") ) students$passed <- students$marks >= 70 students[students$city == "Hyd", ] |
(continue for grade column using ifelse)
Now — which exercise do you want to do first?
Or would you like me to:
- Give you 10 more exercises focused on one topic (e.g. only ggplot2, only dplyr, only statistics)?
- Create a mini-project that combines 4–5 exercises into one script?
- Help you solve one of these if you get stuck?
- Or move to next big topic (loops, functions, R Markdown)?
Just tell me what you want to practice right now — I’m right here with the next code block ready! 🚀📝
