Unique in R – finding unique items

Sometimes you only need the unique elements of a dataset. Fortunately, R has a handy helper function – unique ().

All you need to do is pass the data set object through unique () and it will return only the unique values of that set. This works for lists, vectors, and data frames – among others.

Unique in R – examples

We’re going to dive into a fairly straightforward example. Those of you who have seen our other tutorials may be familar with the needle in a haystack list of items.

# unique in R example
> items <- c('hay','hay','hay','more hay','needle','hay','hay')
> unique(items)
[1] "hay"      "more hay" "needle"  
> special <- unique(items)
> special
[1] "hay"      "more hay" "needle"  

# Results of the unique in r example

How To Count Unique Rows in R

But wait – there’s more. You can easily combine the unique function with other common R functions to handle many common tasks. Consider that time honored puzzler – “how many unique items do we have here?”

And yet – the answer is finally within reach! Merely apply the length function to results of the unique function. That will give you a rather definitive count of how many unique items are in the data object.

# unique in R - counting unique items
> items <- c('hay','hay','hay','more hay','needle','hay','hay')
> length (unique(items))
[1] 3

Applications of Unique in R

As it turns out, this particular function is extremely useful when you’re building programs. Many common mathematical and computer science operations require you to reduce a data set to the unique values within it. This can also be an important way to reduce the total row count of a particular program and thus – complexity, space utilization, and run time.

This is the equivalent of specifying “select distinct” within a SQL query. It produces a similar result (sans tuple) as running a “set’ operation in Python.