You are currently viewing Add or Append Element to Vector in R?

How to add or append an element to a Vector in R? Use the append() function to add a single/multiple elements to the vector. A vector in R is a sequence of data elements of the same data type. Therefore, when you add an element to a Vector, ensure that it is of the same type.

1. Quick Examples of Add Element to Vector

Following are quick examples of how to add or append a single element or multiple elements to a vector in R.


# Quick Examples

# Using R base append()
vec = c('java','python')
vec2 <- append(vec,'r')
print(vec2)

# Add element to vector at specified position
vec = c('java','python')
vec2 <- append(vec,'r',after=1)
print(vec2)

# Add Multiple elements to Vector
vec2 <- c(vec,'r','php')
print(vec2)

# Append vector with another vector
vec = c('java','python')
vec2 = c('r','php')
vec3 <- append(vec,vec2)
print(vec3)

# Append value to empty Vector
empVec <- character()
vec2 = c(empVec,'R')
print(vec2)

2. Syntax of append() to Add Element to Vector

The following is the syntax and usage of the append() function. The append() function returns a vector containing both values of the given vector x and the element specified by the values parameter. By default, it appends a new element at the end of the vector x. If you want to append a new element to a vector x at a specified position you can use the after parameter.


# Syntax of append()
append(x, values, after = length(x))

Parameters:

  • x – Input vector
  • values – string, list, or vector.
  • after – Position to add. Optional. When not specified the element is added at the end of the vector.

Return:

  • Returns a vector after adding an element.

3. Add Element to Vector using append()

By using R base append() function let’s add an element to the existing vector in R. By default, it adds an element at the end of the vector. The following example adds an element r to the vector. Here, I have used c() to create a vector.


# Using R base append()
vec = c('java','python')
vec2 <- append(vec,'r')
print(vec2)

# Output
# [1] "java"   "python" "r" 

4. Add Element to Vector at Specified Position

alternatively, you can use the after parameter of the append() function to specify the position where you want to add the new element to the vector. The following example adds elements after the first position.


# Add element to vector at specified position
vec = c('java','python')
vec2 <- append(vec,'r',after=1)
print(vec2)

# Output
# [1] "java"   "r"      "python"

5. Add Multiple Elements to Vector

append() function doesn’t support adding multiple elements to a vector at a time, hence use c() R base function to add multiple elements to the vector.


# Add Multiple elements to Vector
vec2 <- c(vec,'r','php')
print(vec2)

# Output
# [1] "java"   "python" "r"      "php"   

6. Append Vector with another Vector

If you have two vectors, you can append one vector with another by using the append() function in R.  The following example appends a second vector to the end of the first vector and returns a single vector with elements of both vectors.


# Append vector with another vector
vec = c('java','python')
vec2 = c('r','php')
vec3 <- append(vec,vec2)
print(vec3)

# Output
# [1] "java"   "python" "r"      "php"   

7. Add value to the Empty Vector

c() function can also be used to add a value to the empty vector. Here, the character() function is used to create an empty character vector.


# Append value to empty Vector
empVec <- character()
vec2 = c(empVec,'R')
print(vec2)

# Output
# [1] "R"

Frequently Asked Questions on Add Elements to Vector

How do I add a single element to a vector in R?

You can use the base R c() or the append() function to add a single element to a vector.

How can I add multiple elements to a vector at once?

To add multiple elements to a vector at once you can use the R base combine function c().

What’s the difference between c() and append() for adding elements to a vector?

c() function is used to combine the vectors in R whereas the append() function is used to add an element to a vector at an end position or specified position.

How do I add elements to a vector at specific positions?

By using the after parameter of the append() function you can add a new element to a vector at a specific position.

How can I add elements to a vector without modifying the original vector?

You can create a new vector by adding elements from the original vector along with the new elements.

8. Conclusion

In this article, you have learned how to add or append an element to the vector by using R base append(), c() functions. Also learned to add an element at the specified position, add multiple elements to a vector, and append value to an empty vector with examples. You can find the complete example from this article at Github R Programming Examples Project.

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium