How to Find Unique Values and Sort Them in R Last Updated : 07 May, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Finding and Sorting unique values is a common task in R for data cleaning, exploration, and analysis. In this article, we will explore different methods to find unique values and sort them in R Programming Language. Using sort() with unique()In this approach, we use the unique() function to find unique values in a vector and then use the sort() function to sort them. R sub <- c("MATH", "SCIENCE", "SCIENCE", "ENGLISH", "HINDI", "ENGLISH", "HINDI", "MATH","PHYSICS") # Find unique values and sort them in ascending order val <- sort(unique(sub)) print(val) Output:[1] "ENGLISH" "HINDI" "MATH" "PHYSICS" "SCIENCE"Below are some of the ways by which we can find unique values and sort them sets in RUsing duplicated() and order()Using table() for Frequency CountsUsing distinct() and arrange() from dplyr PackageMethod 1: Using duplicated() and order()In this approach we use duplicated() to find unique rows in a dataframe and order() to sort them based on a specific column. R df <- data.frame( CustomerID = c(101, 102, 103, 101, 104), Amount = c(50, 30, 20, 50, 40) ) df # Remove duplicate customers unique <- df[!duplicated(df$CustomerID), ] # Sort by customers ID sort_df <- unique[order(unique$CustomerID), ] print(sort_df) Output: CustomerID Amount1 101 502 102 303 103 204 101 505 104 40 CustomerID Amount1 101 502 102 303 103 205 104 40Method 2: Using table() from Frequency CountsIn this approach we use table() to get the frequency counts of unique values, and then sort them using the sort() function. R food <- c("Pizza", "Burger", "Pizza", "Salad", "Pizza", "Burger", "Salad", "Pizza", "Burger") # Get frequency counts of unique preferences pre <- table(food) # Sort preferences by count sort_pre <- sort(pre, decreasing = TRUE) print(sort_pre) Output:food Pizza Burger Salad 4 3 2 Method 3: Using distinct() and arrange() from dplyr PackageIn this method we use functions like distinct() and arrange() from dplyr package to find unique values and sort them using arrange() function. R library(dplyr) df <- data.frame( Name = c("Vipul", "Jayesh", "Pratham", "Vipul"), Age = c(25, 30, 25, 35) ) # Find unique values and sort them sort <- df %>% distinct(Name) %>% arrange(Name) print(sort) Output: Name1 Jayesh2 Pratham3 Vipul Comment More infoAdvertise with us Next Article How to Find Unique Values and Sort Them in R A abhaystriver Follow Improve Article Tags : R Language Dev Scripter R Basics Dev Scripter 2024 Similar Reads How to find duplicate values in a list in R In this article, we will see how to find duplicate values in a list in the R Programming Language in different scenarios. Finding duplicate values in a ListIn R, the duplicated() function is used to find the duplicate values present in the R objects. This function determines which elements of a List 3 min read How to Count Unique and Distinct Values in Excel Counting unique values in Excel is a common challenge when working with large datasets, especially for those analyzing data or generating reports. Knowing how to count distinct Excel values accurately is crucial when managing data like customer lists, product inventories, or sales records. This arti 15 min read How to Use the UNIQUE Function in Excel: Step by Step Guide Do you struggle with manually removing duplicates in Excel? The UNIQUE function is here to make your life easier! In this article, youâll learn how to quickly extract unique values from a list or range using the Excel UNIQUE function. Letâs get into the step-by-step process, formula examples, and ev 5 min read How to find duplicate values in a factor in R finding duplicates in data is an important step in data analysis and management to ensure data quality, accuracy, and efficiency. In this article, we will see several approaches to finding duplicate values in a factor in the R Programming Language. It can be done with two methods Using duplicated() 2 min read How to Perform Univariate Analysis in R? In this article, we will discuss how to perform Univariate Analysis in R Programming Language. Univariate Analysis means doing an Analysis of one variable. The analysis of univariate data is thus the simplest form of analysis since the information deals with only one quantity that changes. Example: 3 min read Like