Repository navigation
Expand file tree
/
Copy pathdistinct.Rd
More file actions
65 lines (58 loc) · 1.85 KB
/
Copy pathdistinct.Rd
File metadata and controls
65 lines (58 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/distinct.R
\name{distinct}
\alias{distinct}
\title{Select distinct/unique rows}
\usage{
distinct(.data, ..., .keep_all = FALSE)
}
\arguments{
\item{.data}{a tbl}
\item{...}{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.}
\item{.keep_all}{If \code{TRUE}, keep all variables in \code{.data}.
If a combination of \code{...} is not distinct, this keeps the
first row of values.}
}
\description{
Retain only unique/distinct rows from an input tbl. This is similar
to \code{\link[=unique.data.frame]{unique.data.frame()}}, but considerably faster.
}
\details{
Comparing list columns is not fully supported.
Elements in list columns are compared by reference.
A warning will be given when trying to include list columns in the
computation.
This behavior is kept for compatibility reasons and may change in a future
version.
See examples.
}
\examples{
df <- tibble(
x = sample(10, 100, rep = TRUE),
y = sample(10, 100, rep = TRUE)
)
nrow(df)
nrow(distinct(df))
nrow(distinct(df, x, y))
distinct(df, x)
distinct(df, y)
# Can choose to keep all other variables as well
distinct(df, x, .keep_all = TRUE)
distinct(df, y, .keep_all = TRUE)
# You can also use distinct on computed variables
distinct(df, diff = abs(x - y))
# 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()
df \%>\% distinct(x)
# Values in list columns are compared by reference, this can lead to
# surprising results
tibble(a = as.list(c(1, 1, 2))) \%>\% glimpse() \%>\% distinct()
tibble(a = as.list(1:2)[c(1, 1, 2)]) \%>\% glimpse() \%>\% distinct()
}