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 vectorvalues
– 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
You can use the base R c()
or the append()
function to add a single element to a vector.
To add multiple elements to a vector at once you can use the R base combine function c()
.
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.
By using the after
parameter of the append() function you can add a new element to a vector at a specific position.
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.
Related Articles
- Explain Character Vector in R?
- How to Get Vector Length in R?
- How to Remove NA from Vector?
- How to Create a Vector in R?
- How to Create a DataFrame From Vectors?
- How to Create Empty Vector in R?
- Create Character Vector in R?
- How to Convert Vector to List in R?
- How to Convert List to Vector in R?
- How to Concatenate Vector in R?
- Merge Vector Elements into String in R?
- How to Subset Vector in R?
- How to Sort Vector in R?
- How to Convert List to String?