R Func Unique
# R unique() Function - Remove Duplicates
[ R Language Examples](https://example.com/r/r-examples.html)
The R unique() function is used to remove duplicate values from vectors or data frames, returning unique elements.
unique() is very commonly used for data cleaning and viewing categorical levels.
The syntax format of the unique() function is as follows:
unique(x)
**Parameter Description:**
* **x** Input vector, data frame, or matrix.
## Examples
# Remove duplicate values from a vector
x <-c(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5)
print("Original vector:")
print(x)
print("After removing duplicates:")
print(unique(x))
# View element categories
classes <-c("AGroup", "BGroup", "AGroup", "CGroup", "BGroup", "AGroup", "CGroup")
print("All categories:")
print(unique(classes))
The output of executing the above code is:
"Original vector:" 3 1 4 1 5 9 2 6 5 3 5 "After removing duplicates:" 3 1 4 5 9 2 6 "All categories:" "AGroup" "BGroup" "CGroup"
Using unique() combined with duplicated() can find which values are duplicates:
## Examples
x <-c(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5)
# duplicated() marks whether it's a duplicate
print("Is it a duplicate (appearing for the second time or later):")
print(duplicated(x))
# Find all duplicate values
dupes <- x[duplicated(x)]
print("Values that appear repeatedly:")
print(unique
YouTip