A vector is the most elementary way to store and structure data in R. For now, think of it as a list of numbers, which can be as short as a single number, or as long as about 2 billion(!) numbers. Perhaps you were used to working with lists of numbers already in a spreadsheet application (E.g., a row or column filled with numbers in Microsoft Excel), or statistics package (e.g. a numeric variable in SPSS or SAS). However, in R, vectors have so many applications that go beyond the use of data in the examples I just mentioned. R vectors are the basic building blocks underlying the most fancy dashboards, interactive apps, machine learning models, tables and figures.
Because vectors are such a key concept, we’re going to practise their use and application slowly, step-by-step. For now we’ll just practise numeric vectors (and save other types, such as character vectors, for later).
In this set we’re practising the basics of vectors, i.e. how to create vectors and assign them to a name. It is the first set in a series of five: In the second set (posted next week) we will practise working with vectors. In set three and four we will practise vector arithmetics to e.g. calculate all kinds of statistics, carry out simulations, sort data, or calculate the distance between two cities.
If you can’t wait till all sets are posted: you can find them right now in our ebook Start Here To Learn R – vol. 1: Vectors, arithmetic, and regular sequences. The book also includes the fifth and final set of the series. This final set focuses on the application of the concepts you learned in the first four sets, to real-world data.
Let’s start really easy (don’t worry, we’ll quickly move to more challenging problems) with a vector containing just a single number, which we also call a scalar. Enter a vector in R, by just typing a random number, e.g.
100, at the prompt and hit the Enter key.
Great! You just created your first vector! Now, let’s first enter a vector with more than one number. E.g. a vector containing the numbers 1, 2, 3, 4, 5, in that order. If you enter these numbers just like this, R will respond with an error message. It throws an error, because it needs a little bit more information from our side that we actually want to store those numbers in a vector structure. We have to use the following notation for this:
c(1, 2, 3, 4, 5).
Now, enter a vector with the first 5 even numbers in R, and hit Enter.
Let’s now enter a much longer vector, containing the numbers 1 to 10, 10 times (use copy & paste). What do the numbers between square brackets in the R output mean?
You should be pretty familiar with entering vectors now. You might actually feel a little bored by typing all these numbers. Life would be pretty miserable if we would have to enter data this way over and over again in R. But fortunately, there is a neat solution! We can assign a vector to a variable name such that we can retrieve the data we have entered, conveniently by just typing the name of the variable.
Try to assign a vector containing the numbers 1, 2, 3, 4, 5 to a variable named
a, using the assignment operator (
<-), and see which of the statements below work.
Enter each of the 9 statements one at a time at the prompt, hit Enter, and try to retrieve the contents of
a, by typing
a at the prompt after you entered each statement:
a<-c(1, 2, 3, 4, 5)
a <- c(50, 60, 70, 80, 90)
a -> c(20, 31, 42, 53, 64)
c(5, 6, 7, 9, 10) <- a
c(101, 102, 103, 104, 105) -> a
a < - c(11, 12, 13, 14, 15)
a < -c(100, 99, 88, 77, 66)
assign(a, c(1000, 2000, 3000, 4000, 5000))
assign('a', c(83, 16, 35, 58, 3))
In an R script, you might have created dozens or even hundreds of vectors. In that case, naming them
c etc. is not ideal, because it will be difficult to keep track of what all those letters actually mean. This problem is easily mitigated by using longer, and meaningful, variable names.
Assign the following vectors to a meaningful variable name:
c(2, 4, 6, 8, 10, 12, 14, 16, 20)
c(1, 10, 100, 1000, 10000, 100000)
Create vectors that correspond to the following variables names:
So far, we have created vectors from a bunch of numbers. Instead of numbers, however, you can also enter other vectors, e.g.
c(vector1, vector2, vector3), and string them together.
To practise this, let’s first create three vectors that each contain just 1 element with variable names
r, and values 1, 2, and 3. Then, create a new vector that contains multiple elements, using the scalars we just created. I.e., create a vector
u of length 3, with the subsequent elements of
To play with this a little more, let’s create a longer vector, using only the assignment operator (
c() function, and the vector
u we just created. I.e., create a new vector
u with length 96 that contains the elements of
u as follows: 1, 2, 3, 1, 2, 3, …., 1, 2, 3
Congratulations, you’re done with this set!
May I kindly ask you to share your thoughts on these exercises? This will allow me to further improve the quality of the exercises.
You can share your thoughts simply by adding a comment below. I am particularly interested in:
- Which exercises you liked least and most
- Which (if any) exercises were too hard, and should be simplified
- Which (if any) exercises were too easy and should be more challenging
- Overall comments on the quality of this set
- Which topics you’d like to see addressed in future sets
- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below