R Vectors Exercises: 12 Hands-On Problems with Step-by-Step Answers
Twelve focused exercises on R's most fundamental data structure. You will create vectors, coerce types, index by position, name and logical mask, do vectorised arithmetic, and avoid the two recycling traps that catch almost every beginner. Every problem is runnable right here in the page.
The single most important thing to understand in R is that almost everything is a vector. Scalars are length-one vectors. Data frame columns are vectors. Function arguments are often vectors. Mastering vectors is the one investment that pays off in every other part of R.
Work through the exercises in order. The code blocks share state across the whole page, so variables you create in Exercise 1 are still available in Exercise 12.
Section 1, Creating and inspecting vectors
Exercise 1. Four ways to create a vector
Create the same numeric vector 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 in four different ways: with c(), with :, with seq(), and with seq_len(). Confirm they are equal with identical().
Solution
1:10 and seq_len(10) both produce integer vectors. c(1, 2, ...) and seq(1, 10, by = 1) produce double vectors. The values print the same but the type differs.
Exercise 2. Type coercion in c()
Predict the type of each of these vectors, then check with typeof():
Solution
The coercion hierarchy is logical → integer → double → character. c() picks the most general type present.
Exercise 3. Length, head, tail
Create x <- seq(5, 100, by = 5). Report its length, the first three values, and the last three values.
Solution
Section 2, Indexing
Exercise 4. Positive and range indexing
From x (Exercise 3), extract the 1st, 5th and 10th elements in one call, and then the 11th through 15th elements as a contiguous slice.
Solution
A vector of indices inside [ ] picks those positions in the order given. Ranges are just vectors built with :.
Exercise 5. Negative indexing
From x, return everything except the last two elements. Do it two ways: with a negative index, and with head().
Solution
head() and tail() accept negative counts. This is the idiomatic way to drop trailing or leading elements.
Exercise 6. Logical indexing
Return the elements of x that are strictly greater than 30 and strictly less than 75.
Solution
Use & for element-wise AND and | for element-wise OR. The double forms && and || are for single-value logic in if statements and should not be used for vector filtering.
Exercise 7. Named vectors
Create a named vector of the populations (in millions) of five countries:
Return India's population by name, the values for USA and Nigeria in one call, and the names of all countries with population greater than 300 million.
Solution
Note the last one: you apply the logical index to names(pop), not to pop itself, because you want the names back, not the values.
Section 3, Arithmetic, recycling, and summaries
Exercise 8. Vectorised arithmetic
Create a <- 1:5 and b <- 6:10. Compute a + b, a * b, a^b, and the dot product.
Solution
R has no special dot-product operator (unlike NumPy). sum(a * b) is idiomatic and fast.
Exercise 9. Recycling, the safe case and the trap
Predict the output of each of these. Run them and read the warning on the second one.
Solution
R recycles the shorter vector to match the longer. When lengths divide cleanly, no warning. When they do not, R still recycles but warns, and that is almost always a bug in your code. Treat the warning as an error until you have investigated.
Exercise 10. Summary functions
Using pop from Exercise 7, compute the total population, the mean, the median, the country with the largest population (by name), and the country with the smallest (by name).
Solution
which.max() and which.min() return the index of the max/min, which you then apply to names(pop) to get the label.
Section 4, Missing values and reordering
Exercise 11. NAs and counting
Create v <- c(5, NA, 3, 8, NA, 1, 6, NA, 9). Count the NAs, compute the mean ignoring NAs, and return the vector with NAs replaced by 0.
Solution
is.na(v) returns a logical vector. You can use it to either count, filter, or assign into the NA positions.
Exercise 12. Sorting and ordering
From pop (Exercise 7), return (a) the populations sorted descending, and (b) the countries listed from smallest population to largest.
Solution
sort() reorders the values and keeps the names alongside. order() returns the indices that would produce the sorted order, use it when you need to reorder several parallel vectors (or a data frame) by one of them.
Summary
- Vectors are R's fundamental data structure. Scalars are just length-one vectors.
c()promotes types upward: logical → integer → double → character.- Index with positive integers, negative integers, logicals, or names. Logical indexing is the most common in real analysis.
- Arithmetic is vectorised element-wise. Recycling happens when lengths differ, silent when clean, warned otherwise.
- Handle NAs with
na.rm = TRUEor by masking withis.na(). - Use
sort()to reorder a single vector,order()to produce indices for reordering multiple parallel vectors.