Add or Append Element to Vector in R?

How to add or append an element to Vector in R? Use append() function to add a single element or multiple elements to the vector. A vector in R is a sequence of data elements of the same data type. so when you add an element to a vector make sure you are adding an element 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 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 append() function. The append() function returns a vector containing the values in x with the elements of values appended after the specified element of x. By default, it appends at the end of the vector x.


# 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

Use the after parameter to specify the position where you wanted to add the element on 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() doesn’t support adding multiple elements 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 append() function in R.  The following example appends a second vector to the end of the first vector and returns a single vector with all elements.


# 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 Empty Vector

c() function can also be used to add a value to the empty vector. Here, 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"

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

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

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