append element to a vector in R

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:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

[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 Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top