A dataframe in R is a data structure used to store tabular data in rows and columns. In this tutorial, we will look at how to get the unique values in a column in an R dataframe with the help of some examples.
How do you get unique values from an R dataframe column?
You can use the unique()
function in R to get the unique values from a dataframe column. Pass the column values as an argument to the function.
The following is the syntax –
unique(dataframe[[columan_name]])
We get a vector of unique values in the given column.
Steps to get distinct values in an R dataframe column
Let’s now look at the steps to follow to get distinct values in a dataframe column in R
Step 1 – Create a dataframe
First, we will create a dataframe that we will be using throughout this tutorial.
# create a dataframe employees_df = data.frame( "Name"= c("Jim", "Dwight", "Angela", "Tobi", "Kevin"), "Age"= c(26, 28, 29, 32, 30), "Department"= c("Sales", "Sales", "Accounting", "HR", "Accounting") ) # display the dataframe print(employees_df)
Output:
Name Age Department 1 Jim 26 Sales 2 Dwight 28 Sales 3 Angela 29 Accounting 4 Tobi 32 HR 5 Kevin 30 Accounting
We now have a dataframe containing information about some employees working in an office. The dataframe has columns “Name”, “Age”, and “Department”.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Step 2 – Get the unique values in the column using unique()
To get distinct values in a column, pass the column values vector to the unique()
function as an argument. You can use the [[]]
notation to get a column’s values vector.
Let’s get the distinct departments from the “Department” column in the above dataframe.
# get distinct values in "Department" column dept <- unique(employees_df[["Department"]]) # display the vector print(dept)
Output:
[1] "Sales" "Accounting" "HR"
We get a vector of the unique values in the “Department” column.
Note that you can also use the column index to access a column’s values. For example, in the above dataframe, the index of the “Department” column is 3 (rows and columns in R are indexed starting from 1).
# get distinct values in "Department" column dept <- unique(employees_df[[3]]) # display the vector print(dept)
Output:
[1] "Sales" "Accounting" "HR"
We get the same result as above.
You might also be interested in –
- Create a DataFrame in R
- How to Add a Row to a Dataframe in R?
- How to Add a Column to a Dataframe in R?
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.