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

  1. Open RStudio (local or Posit Cloud or rdrr.io/snippets)
  2. Create a new script
  3. Copy-paste each exercise one by one
  4. Type the solution yourself — don’t just read my answer
  5. Run it → compare with expected result
  6. 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

  1. Calculate mean, median, min, max
  2. How many students scored above 80? (use sum() on logical vector)
  3. 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”
  1. Print the data frame
  2. Add a column passed = marks >= 70
  3. 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:

  1. Filter students with marks > 80
  2. Calculate average marks of students from “Hyd”
  3. Count how many passed

Expected Average Hyd marks: 90 Passed: 4

Exercise 5 – Simple Plot (Base R)

Task Using marks vector from exercise 2:

  1. Create a scatter plot of marks vs position (1 to 8)
  2. Add title “Student Marks”
  3. 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:

R

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:

R

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:

  1. Find 10th, 25th, 50th, 75th, 90th percentile
  2. How many cars have mpg > 90th percentile?
  3. Flag outliers (mpg > 95th percentile or < 5th)

Exercise 9 – Mean vs Median Comparison

Task Create vector:

R
  1. Calculate mean & median
  2. Explain in a comment why they are so different
  3. Remove the highest value → recalculate both

Exercise 10 – Mini Project (Put everything together)

Task Create this data frame:

R

Do the following:

  1. Add column is_hot = temp >= 30
  2. Calculate average temp
  3. Find max & min temp and which day
  4. Count rainy days
  5. Plot temp over days (line + points)
  6. 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

R

Ex 2 solution

R

Ex 3 solution

R

(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! 🚀📝

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *