unique Function in R (2 Examples)
On this page you’ll learn how to remove duplicates from vectors and data frames using the unique function in R.
The article contains this information:
Here’s the step-by-step process.
Definition & Basic R Syntax of unique Function
Definition: The unique R function removes duplicates from data objects.
Basic R Syntax: Please find the basic R programming syntax of the unique function below.
unique(x) # Basic R syntax of unique function |
unique(x) # Basic R syntax of unique function
I’ll show in the following two examples how to use the unique function in R programming. So keep on reading…
Example 1: Return Unique Elements of Vector
Example 1 illustrates how to retain only unique elements of a vector using the unique() function in R. First, we have to create an exemplifying vector:
vec <- c(1, 3, 3, 1, 4, 7, 1) # Create example vector vec # Print example vector # 1 3 3 1 4 7 1 |
vec <- c(1, 3, 3, 1, 4, 7, 1) # Create example vector vec # Print example vector # 1 3 3 1 4 7 1
Our example vector contains seven numeric elements.
Now, we can apply the unique command as shown below:
vec_unique <- unique(vec) # Apply unique() to vector vec_unique # Print updated vector # 1 3 4 7 |
vec_unique <- unique(vec) # Apply unique() to vector vec_unique # Print updated vector # 1 3 4 7
As you can see based on the output of the RStudio console, the unique function extracted only four elements of our original input vector. In other words: three of the vector elements were duplicates.
Example 2: Return Unique Rows of Data Frame
In this Example, I’ll illustrate how to use the unique function to remove duplicate rows of a data matrix. Again, let’s create some example data:
data <- data.frame(x1 = c(1, 1, 2, 3, 4, 2, 2), # Create example data x2 = c("a", "a", "a", "b", "c", "c", "a")) data # Print example data # x1 x2 # 1 1 a # 2 1 a # 3 2 a # 4 3 b # 5 4 c # 6 2 c # 7 2 a |
data <- data.frame(x1 = c(1, 1, 2, 3, 4, 2, 2), # Create example data x2 = c("a", "a", "a", "b", "c", "c", "a")) data # Print example data # x1 x2 # 1 1 a # 2 1 a # 3 2 a # 4 3 b # 5 4 c # 6 2 c # 7 2 a
Our data table consists of seven rows and two columns. The variable x1 is numeric and the variable x2 is a character string.
Now, let’s apply the unique function to these data:
data_unique <- unique(data) # Apply unique() to data frame data_unique # Print updated data frame # x1 x2 # 1 1 a # 3 2 a # 4 3 b # 5 4 c # 6 2 c |
data_unique <- unique(data) # Apply unique() to data frame data_unique # Print updated data frame # x1 x2 # 1 1 a # 3 2 a # 4 3 b # 5 4 c # 6 2 c
The RStudio console returns only five out of the original seven lines of data. The second and seventh rows were removed.
Video, Further Resources & Summary
Would you like to learn more about the application of the unique function? Then I can recommend to watch the following video of my YouTube channel. In the video, I’m explaining the R codes of this post in RStudio:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, I can recommend to have a look at the other articles of this website.
- Unique Rows of Data Frame Based On Selected Columns
- Find Unique Combinations of All Elements from Two Vectors
- Count Unique Values in R
- Find Values Contained in First Vector but not Another
- Remove Duplicated Rows from Data Frame
- R Functions List (+ Examples)
- The R Programming Language
To summarize: This article showed how to apply the unique function in R programming. If you have any additional comments and/or questions, let me know in the comments section below.
Statistics Globe Newsletter