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.
- Construct a logical statement evaluating to
TRUE
for the values you want to remove andFALSE
for the other values in the vector. For example,vec == 3
. - 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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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 –
- Check if an Element is present in an R Vector
- R – Get Index of an Element in a Vector
- R – Remove NA Values from a Vector
- Append Element to a Vector in R
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.