R – Append Elements to a Vector
Use the append() function in R to add one or more elements to a vector. By default, the new values are added at the end, but the after argument can insert them at a specific position.
append() returns a new vector. It does not modify the original vector unless you assign the returned value back to the same variable.
R append() Function Syntax
The syntax of append() is:
append(x, values, after = length(x))
The arguments have the following meanings:
xis the original vector.valuescontains the element or elements to add.afterspecifies the position after which the new values are inserted. Its default value islength(x), so the values are appended at the end.
The after argument uses positions in the original vector. For example, after = 2 inserts the new values after the second element.
Append One Element to the End of an R Vector
In the following example, a character value is appended to the end of a vector. The result is assigned back to x.
> x = c('Max', 'Jack', 'Carl')
> x = append(x, 'Mike')
> print (x)
[1] "Max" "Jack" "Carl" "Mike"
Because no after value is supplied, "Mike" is placed after the last element.
Append Multiple Elements to an R Vector
Pass a vector as the values argument to append several elements at once.
x <- c(10, 20, 30)
result <- append(x, c(40, 50))
print(result)
[1] 10 20 30 40 50
The elements 40 and 50 are added in the same order in which they appear in the values vector.
Concatenate Two Vectors with append()
You can concatenate two vectors by passing the second vector as the values argument.
> x = c('Max', 'Jack', 'Carl')
> values = c('Mike', 'Samanta')
> y = append(x, values)
> print (y)
[1] "Max" "Jack" "Carl" "Mike" "Samanta"
append() returns a new vector containing all elements from x, followed by all elements from values.
Insert Vector Elements at a Specific Position
Use the after argument to insert values inside a vector instead of adding them at the end.
> x = c('Max', 'Jack', 'Carl')
> values = c('Mike', 'Samanta')
> y = append(x, values, 2)
> print (y)
[1] "Max" "Jack" "Mike" "Samanta" "Carl"
Here, after = 2 places "Mike" and "Samanta" after the second element, "Jack".
Insert an Element at the Beginning of a Vector
Set after = 0 to insert a value before the first element.
x <- c(20, 30, 40)
result <- append(x, 10, after = 0)
print(result)
[1] 10 20 30 40
This is useful when a new first element must be added without writing a separate indexing expression.
append() Does Not Modify the Original Vector
R vectors are not changed in place by append(). The function creates and returns a new vector.
x <- c(1, 2, 3)
y <- append(x, 4)
print(x)
print(y)
[1] 1 2 3
[1] 1 2 3 4
The original vector x remains unchanged. To update it, assign the result back with x <- append(x, 4).
append() and c() for Combining R Vectors
Both append() and c() can add values to the end of a vector:
x <- c(1, 2, 3)
append(x, 4)
c(x, 4)
For simple end-to-end concatenation, c() is often shorter. Use append() when the insertion position must be controlled with after.
How R Coerces Types When Appending Values
An atomic vector can contain only one basic data type. If the appended value has a different type, R may coerce all elements to a common type.
x <- c(1, 2, 3)
result <- append(x, "four")
print(result)
typeof(result)
[1] "1" "2" "3" "four"
[1] "character"
Because a character value is appended to a numeric vector, R converts the numeric elements to character values.
Common Questions About Appending Vectors in R
How do I append a value to the end of a vector in R?
Use append(x, value). The default after = length(x) places the value after the final element.
How do I add a value to the beginning of an R vector?
Use append(x, value, after = 0). A zero position inserts the value before the first element.
Does append() change the original vector?
No. append() returns a new vector. Assign its result to a variable if the updated vector must be stored.
What is the difference between append() and c() in R?
c() is commonly used to combine values at the end of a vector. append() additionally supports inserting values at a chosen position through the after argument.
Summary of Appending Elements to an R Vector
Use append(x, values) to add elements at the end of a vector, append(x, values, after = n) to insert them after position n, and after = 0 to add values at the beginning. In this R Tutorial, we learned how to append individual values, combine vectors, control insertion positions, preserve the original vector, and handle type coercion.
TutorialKart.com