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() and seq_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(): The c() function takes one main argument and includes optional arguments. The main argument for c() 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(): The seq() 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 use seq(n) where n is the number you want to stop at (if you do seq(2), it will return the numbers 1 and 2.). If you want to specify where to start or stop, you can use the arguments from = and to =, which tells R where it should start and end. Lastly, if you want to change the increment of your sequence, you can use the by = argument.

  • seq_along(): The seq_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 the test vector from the c() example above, the output of seq_along(test) would be 1 2 3 4.

  • rep(): The rep() function takes two main arguments. The first argument is x =, or the vector/value you plan to repeat. The second argument is times =, the number of times you want to repeat the vector (if you don’t enter a value it defaults to 1). If we use our test variable with the rep() function, we can repeat the values of the vector as many times as we want.

48.4 Other Optional Arguments

  • c(): The optional arguments for c() are recursive = and use.names =. The recursive function is useful when the items you are vectoring includes a list. If you set recursive = TRUE it breaks down the list and converts it into a vector. If recursive = FALSE the vector is saved as a list (by default, recursive = FALSE, and it usually stays that way). The argument use.names = tells R whether or not to keep the names of the lists present when the vector is saved.

  • seq(): The optional arguments for seq() are length.out and along.with. length.out tells R how many numbers you want to be present in the sequence of numbers. For example, if you set length.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. If along.with = 1:3, it will return 3 numbers, split up in equal increments.

  • rep(): The optional argument for rep() are length.out and each. length.out tells R how long it wants the output vector to be, similar to the main argument times. 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.5.4 rep()

1. Using the rep() function, repeat the word “dog” 7 times.

2. Save a vector with the words: “University,” “of,” and “Toronto.” Once that vector is saved, use the each argument in the rep() function and repeat each word 3 times.

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.

seq():

  • Some functions in R can work without specifically defining arguments (ggplot(aes(x, y)) is the same as ggplot(aes(x = x, y = y))). The seq() function needs each argument to be written out if you plan to use more arguments than from and to.

seq_along()

  • 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.

rep()

  • 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:

48.8 Questions

  1. Which of the following functions does not return a vector?
  1. c()
  2. seq()
  3. tibble()
  4. seq_along
  1. If I want to repeat the number 21 100 times which function call should I use?
  1. rep(21, 100)
  2. rep(100, 21)
  3. seq(21, 100)
  4. seq(100, 21)
  1. If I want a vector that contains the numbers 55 to 76, which function should I use?
  1. rep()
  2. c()
  3. seq()
  4. seq_along()
  1. If I have a vector called “time” with 3 entries, what will be returned if I run seq_along(time)?
  1. 3
  2. 123
  3. 321
  4. 1 2 3
  1. True or False, the output from seq(1, 3) is different from seq(3)?
  1. True
  2. False
  1. If I have a vector called “money” which contains 3 elements, how long will output of rep(money, each = 3) be?
  1. 3
  2. 6
  3. 9
  4. 1
  1. If I have a vector called “money” which contains 3 elements, how long will output of rep(money, 3, each = 3) be?
  1. 9
  2. 27
  3. 30
  4. An error will occur
  1. What is the most common cause of the “object ‘string’ not found” error?
  1. The vector you are trying to reference does not exist
  2. The object you are referencing is not a vector
  3. The vector has too many NAs
  4. The function you are using does not exist
  1. What is the output of seq(3, 12, 4)?
  1. 36912
  2. 3 6 9 12
  3. 3711
  4. 3 7 11
  1. What is the output of seq_along(seq(3, 12))?
  1. 1 2 3 4 5 6 7 8 9 10
  2. 12345678910
  3. 10
  4. 1