Find Unique Values in R


Are you working with R language? Do you have a dataframe? How do you find unique values in a column of a dataframe?

Here is a very small dataset called friends. I created it from nothing. It is created manually in the code below. It’s just for demonstration purposes and doesn’t mean anything.

library(skimr)
name <- c("Bob", "Sally", "Pierre", "Pat")
age <- c(40, 41, 42, 43)
gender <- c("M", "F", "M", "F")
friends <- data.frame(name, age, gender)

Below is what the dataframe look like in RStudio.

We know that the unique values for age are 40, 41 and 42. How do we check that in R? Below is the code you can run.

unique(friends$age)

The output is as follows

[1] 40 41 42

Why would we want to find unique values in the first place? We might be doing some exploratory data analysis (EDA). We might be looking for errors or inconsistencies in the data. Suppose you have a list of people that have listed their favorite color and turquoise is one of the choices. You find that three people have chosen teal and 29 have picked turquoise. To keep the list of colors fairly simple, you might decide to change all of the teal to turquoise.

Teal is a darker, cooler color that is closer to blue, while turquoise is a brighter, more vibrant color that is closer to green. Turquoise has a higher saturation than teal, giving it a more intense appearance.

Leave a Reply