In this tutorial, we will look at how to remove duplicates from a vector in R with the help of some examples.
How do I remove duplicates from a vector in R?
You can use the R built-in unique()
function to remove duplicates from a vector. Pass the vector from which you want to remove the duplicates as an argument.
The following is the syntax –
# remove duplicates from vector vec unique(vec)
If you pass a vector to the unique()
function, it will return a vector with the duplicates removed.
You can also use the unique()
function to remove duplicates from an array or a dataframe in R.
Examples
Let’s now look at some examples of using the above function.
Remove duplicates from a vector
Let’s create a vector of some numbers having duplicate values and then apply the unique()
function to see what we get.
# create a vector vec <- c(1, 2, 2, 3, 4, 4, 4) # remove duplicates from vec print(unique(vec))
Output:
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.
[1] 1 2 3 4
You can see that the resulting vector contains only the unique elements from the original vector.
Remove duplicates from a vector with NA values
What would happen if you try to remove duplicates from a vector containing NA
values?
Let’s find out.
# create a vector vec <- c(1, 2, 2, 3, NA, 4, 4, NA, 4) # remove duplicates from vec print(unique(vec))
Output:
[1] 1 2 3 NA 4
Here, we apply the unique()
function on a vector containing some NA
values as well. You can see that the resulting vector contains only distinct values.
We only get one occurrence of NA
inside the resulting vector with the duplicates removed.
If you don’t want the NA
values, you can either remove them before or after applying the unique()
function.
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.