R: How to Find Unique Values and Sort Them


You can use the following methods to find unique values and then sort them in R:

Method 1: Find Unique Values in Vector & Sort Them

#get unique values sorted in ascending order
sort(unique(data))

Method 2: Find Unique Rows in Data Frame & Sort Them

#remove duplicate rows in data frame
df_new = df[!duplicated(df), ]

#display unique rows sorted by values in specific column
df_new = df_new[order(df_new$my_column), ]

The following examples show how to use each method in practice.

Example 1: Find Unique Values in Vector & Sort Them

Suppose we have the following vector in R:

#create vector of values
data <- c(2, 2, 4, 7, 2, 4, 14, 7, 10, 7)

We can use the following syntax to find the unique values in the vector and sort them:

#get unique values sorted in ascending order
sort(unique(data))

[1]  2  4  7 10 14

Notice that the unique values from the vector are returned in ascending order.

We can also use the argument decreasing=TRUE to sort the unique values in descending order:

#get unique values sorted in descending order
sort(unique(data), decreasing=TRUE)

[1] 14 10  7  4  2

Notice that the unique values from the vector are returned in descending order.

Example 2: Find Unique Values in Data Frame & Sort Them

Suppose we have the following data frame in R:

#create data frame
df <- data.frame(team=c('A', 'B', 'A', 'A', 'A', 'B', 'B', 'B', 'A', 'B'),
                 points=c(2, 10, 7, 7, 2, 4, 14, 7, 2, 7))

#view data frame
df

   team points
1     A      2
2     B     10
3     A      7
4     A      7
5     A      2
6     B      4
7     B     14
8     B      7
9     A      2
10    B      7

We can use the following syntax to find the unique rows in the data frame and sort them based on the values in the team column:

#remove duplicate rows in data frame
df_new = df[!duplicated(df), ]

#sort unique rows based on values in team column
df_new = df_new[order(df_new$team, df_new$points), ]

#view new data frame
df_new

  team points
1    A      2
3    A      7
2    B      4
6    B      7
7    B     10
8    B     14

Notice that the unique rows are returned and sorted based on the values in the team column, then by the values in the points column.

Related How to Use sort(), order(), and rank() in R

Additional Resources

The following tutorials explain how to perform other common operations in R:

How to Count Unique Values in Column in R
How to Select Unique Rows in a Data Frame in R
How to Find All Unique Combinations of Two Vectors in R

Leave a Reply

Your email address will not be published. Required fields are marked *