How to Append Data to a Vector in R

04.02.2021

Vectors are one of the most commonly used data structures in R. They support a list of items as long as each item is the same type. In this article, we will learn how to append data to a vector in R.

The first way to append data to a vector in R is to create a new vector.

v = c(1, 2, 3)
new.v = c(v, 4)

We can also use this notation to concatenate vectors together.

a = c(1, 2, 3)
b = c(4, 5, 6)

c = c(a, b)

You can also use the index operator [] to add items to a vector.

a = c(1, 2, 3)
a[4] = 5

Finally, we can also use the append function wiht the after arguement to add data to a vector. The function returns a new vector and takes the vector to append to, the value to add, and the after parameter which is the index where to insert the new data.

a = c(4, 5, 6)

b = append(a, 7, after = 3)