Analogous function for distinct in dplyr

distinct_dt(data, ..., .keep_all = FALSE)

Arguments

data

data.frame

...

Optional variables to use when determining uniqueness. If there are multiple rows for a given combination of inputs, only the first row will be preserved. If omitted, will use all variables.

.keep_all

If TRUE, keep all variables in data.frame. If a combination of ... is not distinct, this keeps the first row of values.

Value

data.table

See also

Examples

iris %>% distinct_dt()
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1: 5.1 3.5 1.4 0.2 setosa #> 2: 4.9 3.0 1.4 0.2 setosa #> 3: 4.7 3.2 1.3 0.2 setosa #> 4: 4.6 3.1 1.5 0.2 setosa #> 5: 5.0 3.6 1.4 0.2 setosa #> --- #> 145: 6.7 3.0 5.2 2.3 virginica #> 146: 6.3 2.5 5.0 1.9 virginica #> 147: 6.5 3.0 5.2 2.0 virginica #> 148: 6.2 3.4 5.4 2.3 virginica #> 149: 5.9 3.0 5.1 1.8 virginica
iris %>% distinct_dt(Species)
#> Species #> 1: setosa #> 2: versicolor #> 3: virginica
iris %>% distinct_dt(Species,.keep_all = TRUE)
#> Species Sepal.Length Sepal.Width Petal.Length Petal.Width #> 1: setosa 5.1 3.5 1.4 0.2 #> 2: versicolor 7.0 3.2 4.7 1.4 #> 3: virginica 6.3 3.3 6.0 2.5
mtcars %>% distinct_dt(cyl,vs)
#> cyl vs #> 1: 6 0 #> 2: 4 1 #> 3: 6 1 #> 4: 8 0 #> 5: 4 0
mtcars %>% distinct_dt(cyl,vs,.keep_all = TRUE)
#> cyl vs mpg disp hp drat wt qsec am gear carb #> 1: 6 0 21.0 160.0 110 3.90 2.620 16.46 1 4 4 #> 2: 4 1 22.8 108.0 93 3.85 2.320 18.61 1 4 1 #> 3: 6 1 21.4 258.0 110 3.08 3.215 19.44 0 3 1 #> 4: 8 0 18.7 360.0 175 3.15 3.440 17.02 0 3 2 #> 5: 4 0 26.0 120.3 91 4.43 2.140 16.70 1 5 2