Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select unique values with 'select' function in 'dplyr' library

Is it possible to select all unique values from a column of a data.frame using select function in dplyr library? Something like "SELECT DISTINCT field1 FROM table1" in SQL notation.

Thanks!

like image 569
nodm Avatar asked Aug 29 '14 15:08

nodm


People also ask

How do I select unique values in a column in R?

To find unique values in a column in a data frame, use the unique() function in R. In Exploratory Data Analysis, the unique() function is crucial since it detects and eliminates duplicate values in the data.

How do I extract unique values in R?

To extract unique elements from Vector, data frame, or array-like R object, use the unique() function. The unique() is an inbuilt R function that returns a vector, data frame, or array-like object but with duplicate elements/rows removed.


1 Answers

In dplyr 0.3 this can be easily achieved using the distinct() method.

Here is an example:

distinct_df = df %>% distinct(field1)

You can get a vector of the distinct values with:

distinct_vector = distinct_df$field1

You can also select a subset of columns at the same time as you perform the distinct() call, which can be cleaner to look at if you examine the data frame using head/tail/glimpse.:

distinct_df = df %>% distinct(field1) %>% select(field1) distinct_vector = distinct_df$field1

like image 127
Ron Gejman Avatar answered Sep 22 '22 18:09

Ron Gejman