remove element from a vector in R

R – Remove Element From a Vector

Vectors are used to store one-dimensional data of the same type in R. When working with vectors, it can be handy to know to remove elements from a vector. In this tutorial, we will look at how to remove an element from a vector in R with the help of some examples.

How to remove an element from a vector in R?

You can filter the vector to remove the element(s) that you do not want in the vector using the [] notation in R.

Let’s now look at some different ways in which you can remove elements from a vector in R.

Remove element by value

Use the following steps to remove an element from a vector using its value. That is, remove all occurrences of the element from the vector.

  1. Construct a logical statement evaluating to TRUE for the values you want to remove and FALSE for the other values in the vector. For example, vec == 3.
  2. Use the ! operator to negate the result from the above logical statement and filter the values that you don’t want using the [] notation. For example, we can use the above logical statement to remove the element 3 from the vector vec, vec[!(vec == 3)].

Let’s look at an example.

# create a vector
vec <- c(1, 2, 3, 4, 3, 5)
# remove element 3 from the vector
print(vec[!(vec == 3)])

Output:

[1] 1 2 4 5

Here, we create a vector of some numbers and use the above-mentioned syntax to remove the occurrences of the element 3 from the vector. You can see that the resulting vector does not have any instances of 3 in it. That is, all the occurrences of 3 are removed in the result.

You can also remove more than one element at a time from a vector. For this, create a vector of the elements you want to remove and use %in% operator. For example, let’s remove elements 3 and 5 from a vector of integers 1 to 5.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# create a vector
vec <- c(1, 2, 3, 4, 5)
# remove elements 3 and 5 from the vector
elements_to_remove = c(3, 5)
print(vec[!(vec %in% elements_to_remove)])

Output:

[1] 1 2 4

You can see that the resulting vector does not have elements 3 and 5.

Remove element by index

You can also remove an element from a vector using its index. For this, create a vector of the index (or indices) of the element(s) you want to remove and then use its negation (with the help of the - operator) inside the [] notation of the vector.

Let’s see an example. We’ll create a vector of characters “a”, “b”, “c”, “d”, and “e” and then remove the element at index 3.

# create a vector
vec <- c("a", "b", "c", "d", "e")
# remove element at index 3 from the vector
idx = c(3)
print(vec[-idx])

Output:

[1] "a" "b" "d" "e"

You can see that the resulting vector does not have the element “c”, which was present at index 3 in the original vector.

You can also remove multiple elements using their respective indexes with this method. For example, let’s create the same vector of characters again and now remove elements at indexes 3 and 5.

# create a vector
vec <- c("a", "b", "c", "d", "e")
# remove elements at indexes 3 and 5 from the vector
idx = c(3, 5)
print(vec[-idx])

Output:

[1] "a" "b" "d"

The resulting vector does not have elements “c” and “e” which were present at the indexes 3 and 5 respectively in the original vector.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top