Built with Tutify.io

    AIStudyLabs

    Add or Append Element to Vector in R?

    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.

    <a id="h-1-quick-examples-of-add-element-to-vector"></a>

    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)
    

    <a id="h-2-syntax-of-append-to-add-element-to-vector"></a>

    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:

    Return:

    <a id="h-3-add-element-to-vector-using-append"></a>

    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"
    

    <a id="h-4-add-element-to-vector-at-specified-position"></a>

    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"
    

    <a id="h-5-add-multiple-elements-to-vector"></a>

    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"
    

    <a id="h-6-append-vector-with-another-vector"></a>

    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"
    

    <a id="h-7-add-value-to-empty-vector"></a>

    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.

    <a id="h-8-conclusion"></a>

    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.

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References