48 c, seq, seq along, and rep
Written by Matthew Wankiewicz and last updated on 7 October 2021.
48.1 Introduction
In this lesson, you will learn how to:
- Learn how to make vectors/lists using
c()
. - Create sequences using the
seq()
andseq_along()
functions. - Replicate values in a vector using
rep()
.
Prerequisite skills include:
- General knowledge of using vectors in R.
Highlights:
-
c()
is useful for making vectors quickly. -
seq()
can help create sequences of numbers. -
seq_along()
can give you the position of a value in a vector. -
rep()
can create a vector with a repeated value.
48.2 The content
As you continue using to use R, you will find that making vectors will be something you use a lot. The c()
function is amazing tool to help us create vectors. The c()
function can make vectors from sets of numbers, letters, or both!
Sometimes, you may find that you want to create different sequences of numbers, either for categorizing variables or just getting a list of random numbers. The seq()
function is one of the best ways to achieve this. The seq()
function gives us a list of numbers usually from 1 to the number you choose. You can also decide if the function skips numbers.
The seq_along()
function is useful for finding the index position of parts of a vector. Sometimes, you will have a vector will different values, and seq_along()
is useful for finding which position the value is in.
Another thing you may attempt to do in R is repeat values. Often times, you will repeat values when you want to make an “empty” vector which will store the results of simulations. The rep()
function allows us to repeat strings or numbers a certain amount of times, which we can then save as a vector.
48.3 Arguments
c()
: Thec()
function takes one main argument and includes optional arguments. The main argument forc()
are the values that you want to save as a vector. The arguments used can be any set of numbers or words, or both. For example we would use an entry could be:test <- c("This", "is", "example", 1)
.seq()
: Theseq()
function can take 1 to 3 entries, depending on how specific you want your set of numbers to be. If you want a list of numbers from 1 to any number, you can simply useseq(n)
where n is the number you want to stop at (if you doseq(2)
, it will return the numbers 1 and 2.). If you want to specify where to start or stop, you can use the argumentsfrom =
andto =
, which tells R where it should start and end. Lastly, if you want to change the increment of your sequence, you can use theby =
argument.seq_along():
Theseq_along()
function takes one argument. The argument it takes is a vector for which it will give the index position of a data point. For example, if we use thetest
vector from thec()
example above, the output ofseq_along(test)
would be 1 2 3 4.rep()
: Therep()
function takes two main arguments. The first argument isx =
, or the vector/value you plan to repeat. The second argument istimes =
, the number of times you want to repeat the vector (if you don’t enter a value it defaults to 1). If we use ourtest
variable with therep()
function, we can repeat the values of the vector as many times as we want.
48.4 Other Optional Arguments
c()
: The optional arguments forc()
arerecursive =
anduse.names =
. The recursive function is useful when the items you are vectoring includes a list. If you setrecursive = TRUE
it breaks down the list and converts it into a vector. Ifrecursive = FALSE
the vector is saved as a list (by default,recursive = FALSE
, and it usually stays that way). The argumentuse.names =
tells R whether or not to keep the names of the lists present when the vector is saved.seq()
: The optional arguments forseq()
arelength.out
andalong.with
.length.out
tells R how many numbers you want to be present in the sequence of numbers. For example, if you setlength.out = 3
R will return 3 numbers, split up in even increments.along.with =
is similar to length out, it takes a vector and returns the same amount of numbers as the length of that vector. Ifalong.with = 1:3
, it will return 3 numbers, split up in equal increments.rep()
: The optional argument forrep()
arelength.out
andeach
.length.out
tells R how long it wants the output vector to be, similar to the main argumenttimes
.each
only applies when you plan to repeat a vector.each()
tells R how many times the first element of the vector should be repeated before it moves on to repeating the second element.
48.5 Questions and Exercises
48.5.1 c()
1.
2. Using the c()
function, save a vector with the numbers 21, 31, and 42 in it. (Add the c() function beside the arrow.)
The c()
function can save numbers but can also save words with numbers, try it out in the next exercise.
3. Save a vector with the numbers 21 and 31 along with the words “Statistics” and “Data”
48.5.2 seq()
1. Using the seq() function, return the sequence of numbers starting at 2 and ending at 20, increasing by 2’s
2.
3. Using the length.out
argument in the seq()
function, return the sequence of FOUR numbers starting at 25 and ending at 100.
48.5.3 seq_along()
You can use the seq_along()
function to find the index position of elements in a vector, run the code chunk below to see how it works
1. Now, create a vector containing the words “stats,” “science,” “math,” “R” and use the seq_along()
function to find the index positions of each word.
48.6 Common mistakes & errors
Here are some common errors that can occur for each of the functions discussed in this section:
c()
:
- Error: object ‘string’ not found This means that R thinks you’re trying to save an existing vector under the name ‘string’ (will likely be different). To fix this, add quotations marks around the word that R is having an issue with. If that doesn’t work, there may be a typo.
- Some functions in R can work without specifically defining arguments (
ggplot(aes(x, y))
is the same asggplot(aes(x = x, y = y))
). Theseq()
function needs each argument to be written out if you plan to use more arguments thanfrom
andto
.
- Error: object ‘string’ not found In this case, it means that the vector that R is trying to find does not exist. This likely means that there was a typo.
- Error: object ‘string’ not found As mentioned above, R does not recognize an object entered into the function. You likely have a typo or you should put quotation marks around the string you want to repeat.
48.7 Next steps
Some next steps include:
Looking at more examples of
rep()
andseq()
in action: https://bookdown.org/ndphillips/YaRrr/vectors.htmlR4DS’s explanation of using iteration (include the
seq_along()
function): https://r4ds.had.co.nz/iteration.html
48.8 Questions
- Which of the following functions does not return a vector?
- If I want to repeat the number 21 100 times which function call should I use?
rep(21, 100)
rep(100, 21)
seq(21, 100)
seq(100, 21)
- If I want a vector that contains the numbers 55 to 76, which function should I use?
- If I have a vector called “time” with 3 entries, what will be returned if I run
seq_along(time)
?
- 3
- 123
- 321
- 1 2 3
- True or False, the output from
seq(1, 3)
is different fromseq(3)
?
- True
- False
- If I have a vector called “money” which contains 3 elements, how long will output of
rep(money, each = 3)
be?
- 3
- 6
- 9
- 1
- If I have a vector called “money” which contains 3 elements, how long will output of
rep(money, 3, each = 3)
be?
- 9
- 27
- 30
- An error will occur
- What is the most common cause of the “object ‘string’ not found” error?
- The vector you are trying to reference does not exist
- The object you are referencing is not a vector
- The vector has too many NAs
- The function you are using does not exist
- What is the output of
seq(3, 12, 4)
?
- 36912
- 3 6 9 12
- 3711
- 3 7 11
- What is the output of
seq_along(seq(3, 12))
?
- 1 2 3 4 5 6 7 8 9 10
- 12345678910
- 10
- 1