Open in App
Not now

# Count Unique Values in R

• Last Updated : 30 May, 2021

In this article, we will see how we can count unique values in R programming language.

Example:

Input:  1 2 3 2 4 5 1 6 8 9 8 6 6 6 6

Output: 8

### Method 1:  Using length(unique()) function

Unique() function when provided with a list will give out only the unique ones from it. Later length() function can calculate the frequency.

Syntax:

length(unique( object )

Example 1:

## R

 `# Sample vector ` `v<-``c``(1,2,3,2,4,5,1,6,8,9,8,6,6,6,6) ` `v ` ` `  `print``(``"Unique values"``) ` ` `  `# count unique elements ` `length``(``unique``(v)) `

Output:

[1] 1 2 3 2 4 5 1 6 8 9 8 6 6 6 6

[1] “Unique values”

[1] 8

With a list with multiple NA value, it will be counted as 1 separate entity.

Example 2:

## R

 `# Sample vector ` `v<-``c``(``NA``,2,``NA``,3,2,4,5,1,6,8,9,8,6,6,6,6) ` `v ` ` `  `print``(``"Unique values"``) ` ` `  `# count unique elements ` `length``(``unique``(v)) `

Output:

[1] NA  2 NA  3  2  4  5  1  6  8  9  8  6  6  6  6

[1] “Unique values”

[1] 9

Example 3:

## R

 `# Sample dataframe ` `df<-``data.frame``(c1=``c``(``NA``,2,``NA``,3,2,4),c2=``c``(5,1,6,6,6,6)) ` `df ` ` `  `print``(``"Unique values"``) ` ` `  `# count unique elements ` `length``(``unique``(df\$c1)) `

Output:

c1 c2

1 NA  5

2  2  1

3 NA  6

4  3  6

5  2  6

6  4  6

[1] “Unique values”

[1] 4

This method is not applicable for matrices

Example 4:

## R

 `# Sample matrix ` `mat<-``matrix``(``c``(``NA``,2,``NA``,3,2,4,5,1,6,6,6,6),ncol=3) ` `mat ` ` `  `print``(``"Unique values"``) ` ` `  `# count unique elements ` `length``(``unique``(mat)) `

Output:

[,1] [,2] [,3]

[1,]   NA    2    6

[2,]    2    4    6

[3,]   NA    5    6

[4,]    3    1    6

[1] “Unique values”

[1] 12

### Method 2: Using table() function

We will be using the table() function to get the count of unique values. The table() function in R Language is used to create a categorical representation of data with the variable name and the frequency in the form of a table.

Syntax

table(object)

Example 1:

## R

 `v <- ``c``(5,``NA``,``NA``,2,3,4,5,3,7,8,9,5) ` `v ` ` `  `print``(``"Count of unique values"``) ` `table``(v) `

Output:

[1]  5 NA NA  2  3  4  5  3  7  8  9  5

[1] “Count of unique values”

v

2 3 4 5 7 8 9

1 2 1 3 1 1 1

Example 2:

## R

 `# Sample dataframe ` `df<-``data.frame``(c1=``c``(``NA``,2,``NA``,3,2,4),c2=``c``(5,1,6,6,6,6)) ` `df ` ` `  `print``(``"Unique values"``) ` ` `  `# count unique elements ` `table``(df\$c2) `

Output:

c1 c2

1 NA  5

2  2  1

3 NA  6

4  3  6

5  2  6

6  4  6

[1] “Unique values”

1 5 6

1 1 4

Example 3:

## R

 `# Sample matrix ` `mat<-``matrix``(``c``(``NA``,2,``NA``,3,2,4,5,1,6,6,6,6),ncol=3) ` `mat ` ` `  `print``(``"Unique values"``) ` ` `  `# count unique elements ` `table``(mat) `

Output:

[,1] [,2] [,3]

[1,]   NA    2    6

[2,]    2    4    6

[3,]   NA    5    6

[4,]    3    1    6

[1] “Unique values”

mat

1 2 3 4 5 6

1 2 1 1 1 4

### Method 3: Converting Output into Dataframe using as.data.frame() function

This method will return the individual frequency of each element.

Syntax:

as.data.frame(table(v))

Example:

## R

 `# Sample vector ` `v<-``c``(1,2,3,2,4,5,1,6,8,9,8,6,6,6,6) ` `v ` ` `  `print``(``"Count of Unique values"``) ` ` `  `as.data.frame``(``table``(v)) `

Output:

[1] 1 2 3 2 4 5 1 6 8 9 8 6 6 6 6

[1] “Count of Unique values”

v Freq

1 1    2

2 2    2

3 3    1

4 4    1

5 5    1

6 6    5

7 8    2

8 9    1

### Method 4: Using aggregate() function

The aggregate() function will always return a data frame that contains all unique values from the input data frame after applying the specific function. We can only apply a single function inside an aggregate function

Syntax: aggregate(data.frame(count = v), list(value = v), length)

Parameters:

formula: the variable(s) of the input data frame we want to apply functions on.

data: the data that we want to use for group by operation.

function: the function or calculation to be applied.

Example:

## R

 `# Sample vector ` `v<-``c``(1,2,3,2,4,5,1,6,8,9,8,6,6,6,6) ` `v ` ` `  `print``(``"Count of Unique values using aggregate() function"``) ` ` `  `aggregate``(``data.frame``(count = v), ``list``(value = v), length) `

Output:

[1] 1 2 3 2 4 5 1 6 8 9 8 6 6 6 6

[1] “Count of Unique values using aggregate() function”

value count

1     1     2

2     2     2

3     3     1

4     4     1

5     5     1

6     6     5

7     8     2

8     9     1

My Personal Notes arrow_drop_up
Related Articles