R Basics Exercises: 15 Practice Problems for Beginners (With Solutions)
15 bite-sized R exercises you can run right here in the browser. Every problem has a worked solution you can reveal, so you get immediate feedback. Work through them in order, they build on each other from your first variable to your first function.
These exercises are designed for complete beginners. If you can install R and open RStudio or Positron, you are ready. Each problem takes two to five minutes. You will learn by writing code, checking the output, comparing against the solution, and then running a small variation of your own.
How to use this page
- Read the problem.
- Try it in the code block below. Click Run to see your output.
- If you get stuck, expand Solution to see a worked answer.
- Before moving on, change one thing in the solution and run it again. The understanding comes from the variation, not the copy.
Section 1, Variables, arithmetic and types
Exercise 1. Assign and print
Create a variable x holding the number 17 and a variable y holding 5. Print their sum, difference, product, and integer quotient (%/%).
Solution
Exercise 2. Types and coercion
Create a <- 3.14, b <- 3L, c <- "3", d <- TRUE. Use class() on each. Then compute b + d, predict the result before running it.
Solution
The key lesson: logical values become 1 and 0 in arithmetic. This is useful for counting TRUE values with sum().
Exercise 3. Round, floor, ceiling
Given p <- 3.87, produce 3.87 rounded to 1 decimal, the floor, the ceiling, and the result truncated toward zero.
Solution
Section 2, Vectors and indexing
Exercise 4. Create and inspect a vector
Create a vector temps containing the values 22, 25, 19, 30, 28, 24, 21. Find its length, mean, and sort it in descending order.
Solution
Exercise 5. Positive indexing
From temps, extract the first value, the last value, and the third through fifth values (inclusive).
Solution
length(temps) is the portable way to get the last element, it works regardless of how long the vector is.
Exercise 6. Logical indexing
Return only the values in temps greater than 23.
Solution
Logical indexing is the most important R idiom: build a TRUE/FALSE vector the same length as the data, then use it as an index. Almost all filtering in R boils down to this.
Exercise 7. Negative indexing
Return temps with the second and fourth elements removed.
Solution
Exercise 8. Named vectors
Create a named vector scores with three entries: alice = 87, bob = 72, carol = 95. Then extract Bob's score two different ways.
Solution
Both return the same value with the name attached. Use name-based indexing when you want the code to still work if the order changes.
Section 3, Sequences and functions
Exercise 9. Build sequences
Create (a) the integers 1 through 20, (b) the even integers from 2 to 20, and (c) a sequence of five equally spaced values from 0 to 1.
Solution
seq() is the Swiss Army knife: pick by = for a step or length.out = for a count, not both.
Exercise 10. Vectorised arithmetic
Create x <- 1:5. Compute x squared, x plus 10, and x minus its own mean. Do all three in one line each, with no loops.
Solution
R applies scalar operations element by element on vectors. Explicit for loops are almost never needed for this kind of work.
Exercise 11. Missing values
Create v <- c(4, NA, 7, 2, NA, 10). Compute its mean naively, then correctly, and count how many NAs it contains.
Solution
Forgetting na.rm = TRUE is the single most common source of "my mean is NA" confusion for beginners. Every summary function (sum, median, min, max, sd) takes the same argument.
Exercise 12. Your first function
Write a function celsius_to_fahrenheit(c) that converts a temperature in Celsius to Fahrenheit using the formula F = C * 9/5 + 32. Test it with 0, 100, and temps from Exercise 4.
Solution
Notice the function works on a vector with zero modification, because the arithmetic inside it is already vectorised.
Section 4, A few intentional traps
Exercise 13. Integer vs double
Predict the output: identical(1, 1L). Then predict identical(1:3, c(1, 2, 3)). Run both.
Solution
identical() checks type as well as value. For numerical equality use == or all.equal().
Exercise 14. The double-equals trap
Create a <- 0.1 + 0.2. Check whether a == 0.3. Explain the result, then compute it correctly with all.equal().
Solution
Never compare doubles with ==. Use all.equal() wrapped in isTRUE() inside conditionals.
Exercise 15. Vector recycling
Predict the output of c(1, 2, 3, 4, 5, 6) + c(10, 20). Then predict c(1, 2, 3, 4, 5) + c(10, 20). Run both and read the warning carefully.
Solution
R silently recycles when lengths divide evenly and warns when they do not. This is the source of many hard-to-find bugs, respect the warning.
Summary
- R uses
<-for assignment, with typed values (numeric, integer, character, logical) that coerce silently into each other. - Vectors support positive, negative, logical, and name-based indexing. Logical indexing is the most important.
- Arithmetic is vectorised, operations apply element-wise with no loops.
NApropagates; always decide whether to remove it withna.rm = TRUE.- Never compare floats with
==, useall.equal()instead. - Vector recycling is powerful but silently dangerous when lengths do not align.