Learning R takes time and practice. If you’re new to programming with the R language, you’re probably taking online courses and reading books on the subject. That’ll give you some of the knowledge you need to become a skilled developer, but you still need to get in some practice writing actual code, executing it, and chasing down bugs.
Still, that doesn’t mean you have to build a complete project every time you want to get your hands dirty writing code. You can test and expand your skills by taking code challenges — minor problems you can solve with code. Some will be harder than others, and each will teach you skills you’ll use later in full-scale projects.
This article introduces you to code challenges by giving you 10 that you can complete using the R programming language. Try a few today and then complete them throughout the week. Once you’ve completed these, we’ll show you how to find more, as well as how to learn more about R.
We designed these code challenges for new R developers, so even if you’re only familiar with the basics, you should have no problem getting started. Just fire up your R development environment and start writing code. Or, try building them in a workspace.
If you have trouble with the challenges or don’t know R yet and want to learn, check out Learn R. This course will teach you everything you need to know to finish these exercises.
1. Create an array of matrixes
In R, a matrix is a collection of elements of the same data type arranged in a fixed number of rows and columns. A vector is the simplest type of data collection, which consists of a collection of things.
For this challenge, you need to write code in R that’ll create an array of two 3×3 matrixes from two given numeric vectors of different lengths. Each matrix will have 3 rows and 3 columns.
2. Create a list with different types
A list in R is an object with heterogeneous elements. A list can consist of many kinds of objects, including matrixes, functions, or even data frames.
For this challenge, create a list with R containing two vectors, one simply x and the other of 100 random integers, and print the results.
3. Create a matrix from vectors
For this code challenge, you should write code in R to create three vectors: x, y, and z. These vectors should contain three elements each, and they all should be integers.
Then, add code to combine the three vectors into a 3×3 matrix, A, where each column represents a vector. Then, change the row names to a, b, and c.
4. Accessing elements in a list
For this code challenge, write code in R that creates a vector of any length you choose, a matrix of any size you want, and a list of any length. Then, add these three objects you created to another list and access the matrix from the list you just created.
5. Classic fizz buzz challenge
This is a classic coding challenge that you might run into in many programming courses and often in entry-level programming job interviews. To complete it, write code in R that prints the numbers from 1 to 100. Also, have it print fizz when the number is a multiple of 3, buzz when it’s a multiple of 2, and fizzbuzz when the number is a multiple of both.
6. Custom number sequences
For this challenge, write code in R that generates a sequence of numbers from 20 to 70. Then, create a variable to hold the mean of the numbers from 23 to 97.
Next, create a variable that holds the sum of the numbers from 34 to 89. Finally, create a list containing these three items.
7. Generate the first 10 numbers in the Fibonacci sequence
A Fibonacci sequence is a series of numbers where each number is the sum of the two numbers before it. For this coding challenge, write code in R that prints out the first 10 numbers in the Fibonacci sequence. It should print the following:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34
Do this mathematically and don’t just copy and paste the numbers above. Can you print out the first 50 for extra credit?
8. Give me the primes
Prime numbers include 2, 3, 5, 7, and any other number that’s only divisible by itself and 1. Write a function in R that takes an integer as a parameter and returns all the prime numbers up to and including that integer.
9. Taking user input
For this challenge, write code in R that prompts a user for their name on the command line. Then, have it prompt them for the city they live in. Finally, print out a greeting. If their name is Bob and their city is Austin, the greeting should read “Hi to Bob from Austin.”
10. Compute them all
In this challenge, you need to create a vector that contains 8 random integer values from -100 to 100. Then, put them in 4×4 matrixes where each column contains two of the generated values, their sum, and their mean.
More ways to build your R coding skills
If you liked the code challenges above and want to try out more, you can find weekly and daily code challenges here. Or, to take your R skills to the next level, you can check out courses like:
Also, remember to continue to complete code challenges often, so your R programming skills never get rusty!