append() method in detail
The append() function in R is used to insert values into a vector at a specified position. If no position is mentioned, the values are added at the end of the vector.
Syntax:
append(x, value, index(optional))
Return Value: It returns a new vector with the specified values appended.
Example 1: Appending a Value at the End of a Vector
vec <- c(2, 4, 6, 8)
# Appending value 12 to the vector
result <- append(vec, 12)
print(result)
Output:
[1] 2 4 6 8 12
Example 2: Inserting a Value at a Specific Position
vec <- c(5, 10, 15, 20)
# Inserting 7 at the second position
result <- append(vec, 7, after = 1)
print(result)
Output:
[1] 5 7 10 15 20
Leave a Reply