Adding and removing elements from a Vector is one of the most used operations in R language. In this tutorial, we will see how to append single or multiple elements to a Vector using different approaches.
Append in R
To append elements to a Vector in R, use the append() method. The append() is a built-in method that adds the various integer values into a Vector in the last position.
append(x, value, index(optional))
x: It is the vector to which values are appended.
value: It is the value that needs to be added in the modified vector.
index: It is a subscript, after which the values are to be appended.
The append() method returns a vector containing the values in x with the elements of values appended after the specified element of x.
Define a vector and append the elements to that vector using the append() method.
data <- 4:8 cat(data, "\n") modified_data <- append(data, 11) cat("After appending an element to a vector", "\n") cat(modified_data)
4 5 6 7 8 After appending an element to a vector 4 5 6 7 8 11
You can see that we appended the “11″ element to a data vector at the end of the vector. This is because the append() method adds the element at the end of a Vector.
Appending multiple elements to the Vector
To append multiple elements to a Vector in R, use the append() method and pass the vector to the existing vector. It will spread out in the existing vector and add multiple elements to that vector.
Let’s say we have two vectors: v1 and v2. We need to add all the elements of v2 to v1, and to do that, and we pass the v2 vector to append() function.
v1 <- 4:8 v2 <- 9:11 cat("Before appending elements to v1", "\n") cat(v1, "\n") modified_data <- append(v1, v2) cat("After appending multiple elements to a v1", "\n") cat(modified_data)
Before appending elements to v1 4 5 6 7 8 After appending multiple elements to a v1 4 5 6 7 8 9 10 11
We add multiple elements to the existing vector by appending a new vector consisting of all our elements.
Adding element at the specific position in Vector
To append an element at the specific position in Vector, pass the index parameter to the append() function. The index is an optional parameter that can be used to add an element after the index.
v1 <- 4:8 v2 <- 11 cat("Before adding element to v1", "\n") cat(v1, "\n") cat("Appending element after index 2", "\n") modified_data <- append(v1, v2, 2) cat(modified_data)
Before adding element to v1 4 5 6 7 8 Appending element after index 2 4 5 11 6 7 8
You can see that we added an element “11” after index 2 of the vector.
Appending element using c() function
c(…, recursive = FALSE, use.names = TRUE)
…: It represents objects to be concatenated.
recursive: It is logical. If recursive = TRUE, the function recursively descends through lists (and pairlists), combining all their elements into a vector.
use.names: It is a logical argument indicating if names should be preserved.
The c() method returns NULL or an expression or a vector of an appropriate mode.
Create a Vector using the colon(:) operator and add an element to that vector using the c() function.
v1 <- 4:8 v2 <- 11 data <- c(v1, v2) cat(data)
4 5 6 7 8 11
And it appends an element at the end of the vector.
That’s it for this tutorial.
Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.