R Beginner Exercises: 50 Practice Problems for Newcomers

Fifty beginner-friendly R practice problems covering vectors, arithmetic, basic statistics, control flow, functions, plotting, and simple file I/O. Hidden solutions.

RRun this once before any exercise
# No external packages needed for most exercises

  

Section 1. Vectors and basics (10 problems)

Exercise 1.1: Create a vector

Difficulty: Beginner. Make a numeric vector of 1 through 10.

Show solution
RInteractive R
v <- 1:10 v

  

Exercise 1.2: c() to combine

Difficulty: Beginner.

Show solution
RInteractive R
c(2, 4, 6, 8)

  

Exercise 1.3: Length

Difficulty: Beginner.

Show solution
RInteractive R
length(c(1, 5, 9, 12))

  

Exercise 1.4: Vector indexing

Difficulty: Beginner. Get element 3.

Show solution
RInteractive R
v <- c(10, 20, 30, 40, 50) v[3]

  

Exercise 1.5: Negative indexing

Difficulty: Beginner. All except element 2.

Show solution
RInteractive R
v <- c(10, 20, 30, 40, 50) v[-2]

  

Exercise 1.6: Range indexing

Difficulty: Beginner. Elements 2 to 4.

Show solution
RInteractive R
v <- c(10, 20, 30, 40, 50) v[2:4]

  

Exercise 1.7: Logical indexing

Difficulty: Beginner. Values > 25.

Show solution
RInteractive R
v <- c(10, 20, 30, 40, 50) v[v > 25]

  

Exercise 1.8: Named vector

Difficulty: Beginner.

Show solution
RInteractive R
c(a = 1, b = 2, c = 3)

  

Exercise 1.9: seq()

Difficulty: Beginner. Sequence 0 to 1 by 0.1.

Show solution
RInteractive R
seq(0, 1, by = 0.1)

  

Exercise 1.10: rep()

Difficulty: Beginner. "a" repeated 5 times.

Show solution
RInteractive R
rep("a", 5)

  

Section 2. Arithmetic and basic stats (10 problems)

Exercise 2.1: Sum

Difficulty: Beginner.

Show solution
RInteractive R
sum(c(1, 2, 3, 4, 5))

  

Exercise 2.2: Mean

Difficulty: Beginner.

Show solution
RInteractive R
mean(c(2, 4, 6, 8))

  

Exercise 2.3: Median

Difficulty: Beginner.

Show solution
RInteractive R
median(c(1, 5, 3, 7, 2))

  

Exercise 2.4: SD

Difficulty: Beginner.

Show solution
RInteractive R
sd(c(2, 4, 6, 8, 10))

  

Exercise 2.5: Min and max

Difficulty: Beginner.

Show solution
RInteractive R
v <- c(5, 2, 8, 1, 9) c(min(v), max(v))

  

Exercise 2.6: Range

Difficulty: Beginner.

Show solution
RInteractive R
range(c(5, 2, 8, 1, 9))

  

Exercise 2.7: Variance

Difficulty: Beginner.

Show solution
RInteractive R
var(c(2, 4, 6, 8, 10))

  

Exercise 2.8: Vectorized arithmetic

Difficulty: Beginner. Multiply each by 2.

Show solution
RInteractive R
c(1, 2, 3) * 2

  

Exercise 2.9: Element-wise sum

Difficulty: Beginner.

Show solution
RInteractive R
c(1, 2, 3) + c(10, 20, 30)

  

Exercise 2.10: Round

Difficulty: Beginner. Round 3.14159 to 2 decimal places.

Show solution
RInteractive R
round(3.14159, 2)

  

Section 3. Data frames (8 problems)

Exercise 3.1: Create a data frame

Difficulty: Beginner.

Show solution
RInteractive R
df <- data.frame(name = c("A","B","C"), age = c(25, 30, 35)) df

  

Exercise 3.2: Access a column with $

Difficulty: Beginner.

Show solution
RInteractive R
mtcars$mpg

  

Exercise 3.3: Number of rows and columns

Difficulty: Beginner.

Show solution
RInteractive R
dim(mtcars)

  

Exercise 3.4: Column names

Difficulty: Beginner.

Show solution
RInteractive R
names(mtcars)

  

Exercise 3.5: First 5 rows

Difficulty: Beginner.

Show solution
RInteractive R
head(mtcars, 5)

  

Exercise 3.6: Filter rows

Difficulty: Beginner. mpg > 25.

Show solution
RInteractive R
mtcars[mtcars$mpg > 25, ]

  

Exercise 3.7: Add a column

Difficulty: Beginner.

Show solution
RInteractive R
mt <- mtcars mt$kpl <- mt$mpg * 0.425 head(mt[, c("mpg","kpl")])

  

Exercise 3.8: Summary

Difficulty: Beginner.

Show solution
RInteractive R
summary(mtcars$mpg)

  

Section 4. Control flow (6 problems)

Exercise 4.1: if/else

Difficulty: Beginner.

Show solution
RInteractive R
x <- 7 if (x > 5) "big" else "small"

  

Exercise 4.2: ifelse vectorized

Difficulty: Beginner.

Show solution
RInteractive R
ifelse(c(2, 7, 4) > 5, "big", "small")

  

Exercise 4.3: for loop

Difficulty: Beginner.

Show solution
RInteractive R
for (i in 1:3) print(i^2)

  

Exercise 4.4: while loop

Difficulty: Beginner.

Show solution
RInteractive R
i <- 1 while (i <= 3) { print(i); i <- i + 1 }

  

Exercise 4.5: break

Difficulty: Intermediate.

Show solution
RInteractive R
for (i in 1:10) { if (i > 5) break; print(i) }

  

Exercise 4.6: next (skip)

Difficulty: Intermediate.

Show solution
RInteractive R
for (i in 1:5) { if (i == 3) next; print(i) }

  

Section 5. Functions (6 problems)

Exercise 5.1: Square function

Difficulty: Beginner.

Show solution
RInteractive R
sq <- function(x) x^2 sq(5)

  

Exercise 5.2: Default argument

Difficulty: Beginner.

Show solution
RInteractive R
multiply <- function(x, n = 10) x * n multiply(5)

  

Exercise 5.3: Multiple arguments

Difficulty: Beginner.

Show solution
RInteractive R
add <- function(a, b) a + b add(3, 4)

  

Exercise 5.4: Return a vector

Difficulty: Beginner.

Show solution
RInteractive R
stats <- function(x) c(min = min(x), mean = mean(x), max = max(x)) stats(c(2, 4, 6, 8))

  

Exercise 5.5: Anonymous function

Difficulty: Beginner.

Show solution
RInteractive R
sapply(1:5, function(x) x^2)

  

Exercise 5.6: \() shorthand

Difficulty: Intermediate.

Show solution
RInteractive R
sapply(1:5, \(x) x^2)

  

Section 6. Plotting (6 problems)

Exercise 6.1: Scatter plot

Difficulty: Beginner.

Show solution
RInteractive R
plot(mtcars$wt, mtcars$mpg)

  

Exercise 6.2: Histogram

Difficulty: Beginner.

Show solution
RInteractive R
hist(mtcars$mpg)

  

Exercise 6.3: Boxplot

Difficulty: Beginner.

Show solution
RInteractive R
boxplot(mpg ~ cyl, data = mtcars)

  

Exercise 6.4: Bar chart

Difficulty: Beginner.

Show solution
RInteractive R
barplot(table(mtcars$cyl))

  

Exercise 6.5: Line chart

Difficulty: Beginner.

Show solution
RInteractive R
plot(1:10, type = "l")

  

Exercise 6.6: Title and axis labels

Difficulty: Beginner.

Show solution
RInteractive R
plot(mtcars$wt, mtcars$mpg, main = "Weight vs MPG", xlab = "Weight", ylab = "MPG")

  

Section 7. NA and types (4 problems)

Exercise 7.1: Detect NA

Difficulty: Beginner.

Show solution
RInteractive R
is.na(c(1, NA, 3))

  

Exercise 7.2: Mean ignoring NA

Difficulty: Beginner.

Show solution
RInteractive R
mean(c(1, NA, 3, 5), na.rm = TRUE)

  

Exercise 7.3: Class of an object

Difficulty: Beginner.

Show solution
RInteractive R
class(1.5) class("hello") class(TRUE)

  

Exercise 7.4: Coerce to numeric

Difficulty: Beginner.

Show solution
RInteractive R
as.numeric(c("1.5", "2.7"))

  

What to do next

  • dplyr-Exercises (shipped), modern wrangling.
  • R-for-Data-Science-Exercises (shipped), broader practice mapped to R4DS topics.
  • R-Interview-Questions (shipped), once basics feel solid.