Finding unique values may be a necessity while analyzing a data set. In this tutorial, we will learn four ways of finding unique values. Find out how to find unique values in R.

In this tutorial, we learn how to find unique values in R. Firstly, we go over unique() function. Secondly, we learn how to use duplicated() function to obtain unique values. Thirdly, we learn how to use distinct() function available in dplyr package (Wickham et al., 2022). At last, we go over names() function to find unique values in R.

Let’s construct an example data including duplicated observations to illustrate how to find unique values in R.

```
data <- c("apple","banana","banana","carrot","carrot","carrot")
data
## [1] "apple" "banana" "banana" "carrot" "carrot" "carrot"
```

**Check Out:***How to Clean Data in R*

### 1) How to Find Unique Values with unique() Function in R

In this part, we use unique() function to find unique values in R.

```
unique(data)
## [1] "apple" "banana" "carrot"
```

**Also Check:** *How to Use apply Functions in R*

### 2) How to Find Unique Values with dublicated() Function in R

In this section, we use duplicated() function to obtain duplicated values. Then, we add the sign ! before the duplicated() function in the data to obtain unique values in R.

```
data[!duplicated(data)]
## [1] "apple" "banana" "carrot"
```

**Also Check:** *How to Recode Character Variables in R*

### 3) How to Find Unique Values with distinct() Function in R

In this section, we learn distinct() function available in dplyr package (Wickham et al., 2022) to learn how to obtain unique values in R.

```
library(dplyr)
distinct(data.frame(data))[,1]
## [1] "apple" "banana" "carrot"
```

### 4) How to Find Unique Values with names() Function in R

At last, we construct the frequency table by table() function. Then, we read the names of observations with names() function to find unique values in R.

```
names(table(data))
## [1] "apple" "banana" "carrot"
```

The application of the codes is available in our youtube channel below.

**Don’t forget to check:** *What are Data Types in R?*

**References**

Wickham, H., Francois, R., Henry, L., Muller, K. (2022). dplyr: A Grammar of Data Manipulation. R package version 1.0.10.

## Leave a Reply