Skip to Content

Append Element to a Vector in R

A vector is a commonly used data structure in R used to store a sequence of values of the same type. When working with vectors, it can be handy to know how to quickly add values to a vector in R. In this tutorial, we will look at how to append an element to an existing vector in R with the help of some examples.

How to append an element to a vector in R?

You can use the R append() function to append a value to a vector. Pass the vector and the value as arguments to the function. The following is the syntax.

# append values to a vector in R
append(x, values, after)

It returns a vector with the appended value. Let’s take a closer look at the arguments for this function.

  • x – The vector to which you want to append the value(s).
  • values – The value (or values) you want to append to the vector.
  • after – (Optional) The index after which to append the value in the vector. It is equal to the length of the vector by default. That is, by default, it appends the value(s) to the end of the vector.

Examples

Let’s look at some examples of using the above syntax to add a value to a vector in R. First, we will create a vector that we will be using throughout this tutorial.

# create a vector
vec <- c(1, 2, 3)
# display the vector
print(vec)

Output:

[1] 1 2 3

We now have a vector of some integer values.

Append value to an R vector

By default, the append() function adds the value to the end of the vector (that is, it appends the value). For example, let’s append the value 4 to the above vector. For this, Pass the vector and the value to append as arguments to the append() function.

# append 4 to the vector
vec <- append(vec, 4)
# display the vector
print(vec)

Output:

[1] 1 2 3 4

You can see that the resulting vector has the value 4 at the end.

Alternatively, you can also use the c() function in R to append a value to a vector. The c() function (or the concatenation function) is commonly used to combine values together in R.

Let’s use the c() function to perform the same operation as above. We create a vector of numbers 1 to 3 and then append 4 to it using the c() function.

# create a vector
vec <- c(1, 2, 3)
# append 4 to the vector
vec <- c(vec, 4)
# display the vector
print(vec)

Output:

[1] 1 2 3 4

We get the same result as above. The value 6 is appended to the end of the vector.

Append element at a given index in an R vector

The append() function also has an additional parameter, after using which you can specify the index after which you want to append the given element. For example, let’s append 5 to the above vector (having values from 1 to 4) after index 2.

# append value after specific index
vec <- append(vec, 5, 2)
# display the vector
print(vec)

Output:

[1] 1 2 5 3 4

You can see that the value 5 is added to the vector in the 3rd index (that is after index 2).

In this tutorial, we looked at two methods to append values to a vector in R. The c() function is commonly used to combine a value with another value. The append() function, on the other hand, also allows you to append a value at a specific position in a vector.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.