The “unique” Function in R
Package: Base R (No specific package)
Purpose: To extract unique elements from a vector, data frame, or other structures.
General Class: Data Manipulation
Required Argument(s):
x: The input vector, data frame, or other structure from which unique elements are to be extracted.
Notable Optional Arguments:
incomparables: A vector of values that should be treated as incomparable when identifying unique elements.
Example:
# Example usage with a vector
my_vector <- c(1, 2, 3, 1, 2, 4, 5)
unique_values <- unique(my_vector)
print(unique_values)
# Example usage with a data frame
my_data_frame <- data.frame(
Name = c("John", "Jane", "Bob", "John", "Alice"),
Age = c(25, 30, 22, 25, 28)
)
unique_rows <- unique(my_data_frame)
print(unique_rows)In this example, unique is a built-in function in base R, and it can be used to find unique elements from vectors or rows in a data frame. The incomparables argument is optional and allows you to specify values that should be treated as incomparable when identifying unique elements.