Select only unique/distinct rows from a data frame. This is similar
to unique.data.frame()
but considerably faster.
distinct(.data, ..., .keep_all = FALSE)
.data | A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). See Methods, below, for more details. |
---|---|
... | < |
.keep_all | If |
An object of the same type as .data
. The output has the following
properties:
Rows are a subset of the input but appear in the same order.
Columns are not modified if ...
is empty or .keep_all
is TRUE
.
Otherwise, distinct()
first calls mutate()
to create new columns.
Groups are not modified.
Data frame attributes are preserved.
This function is a generic, which means that packages can provide implementations (methods) for other classes. See the documentation of individual methods for extra arguments and differences in behaviour.
The following methods are currently available in loaded packages:
dbplyr (tbl_lazy
), dplyr (data.frame
)
.
#> [1] 100#> [1] 69#> [1] 69distinct(df, x)#> # A tibble: 10 x 1 #> x #> <int> #> 1 2 #> 2 10 #> 3 7 #> 4 4 #> 5 9 #> 6 6 #> 7 1 #> 8 3 #> 9 5 #> 10 8distinct(df, y)#> # A tibble: 10 x 1 #> y #> <int> #> 1 3 #> 2 1 #> 3 9 #> 4 10 #> 5 2 #> 6 8 #> 7 4 #> 8 7 #> 9 5 #> 10 6# You can choose to keep all other variables as well distinct(df, x, .keep_all = TRUE)#> # A tibble: 10 x 2 #> x y #> <int> <int> #> 1 2 3 #> 2 10 1 #> 3 7 9 #> 4 4 10 #> 5 9 10 #> 6 6 4 #> 7 1 2 #> 8 3 2 #> 9 5 10 #> 10 8 7distinct(df, y, .keep_all = TRUE)#> # A tibble: 10 x 2 #> x y #> <int> <int> #> 1 2 3 #> 2 10 1 #> 3 7 9 #> 4 4 10 #> 5 7 2 #> 6 10 8 #> 7 6 4 #> 8 9 7 #> 9 2 5 #> 10 6 6#> # A tibble: 10 x 1 #> diff #> <int> #> 1 1 #> 2 9 #> 3 2 #> 4 6 #> 5 5 #> 6 8 #> 7 0 #> 8 7 #> 9 3 #> 10 4#> # A tibble: 67 x 3 #> hair_color skin_color eye_color #> <chr> <chr> <chr> #> 1 blond fair blue #> 2 NA gold yellow #> 3 NA white, blue red #> 4 none white yellow #> 5 brown light brown #> 6 brown, grey light blue #> 7 brown light blue #> 8 NA white, red red #> 9 black light brown #> 10 auburn, white fair blue-gray #> # … with 57 more rows# Grouping ------------------------------------------------- # The same behaviour applies for grouped data frames, # except that the grouping variables are always included df <- tibble( g = c(1, 1, 2, 2), x = c(1, 1, 2, 1) ) %>% group_by(g) df %>% distinct(x)#> # A tibble: 3 x 2 #> # Groups: g [2] #> g x #> <dbl> <dbl> #> 1 1 1 #> 2 2 2 #> 3 2 1