% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/distinct.R
\name{distinct}
\alias{distinct}
\title{Subset distinct/unique rows}
\usage{
distinct(.data, ..., .keep_all = FALSE)
}
\arguments{
\item{.data}{A \code{data.frame}.}

\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}{\code{logical(1)}. If \code{TRUE}, keep all variables in \code{.data}. If a combination of \code{...} is not distinct,
this keeps the first row of values.}
}
\value{
A \code{data.frame} with the following properties:
\itemize{
\item Rows are a subset of the input but appear in the same order.
\item Columns are not modified if \code{...} is empty or \code{.keep_all} is \code{TRUE}. Otherwise, \code{distinct()} first calls \code{mutate()}
to create new columns.
\item Groups are not modified.
\item \code{data.frame} attributes are preserved.
}
}
\description{
Select only distinct/unique rows from a \code{data.frame}.
}
\examples{
df <- data.frame(
  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)

# You 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 <- data.frame(
  g = c(1, 1, 2, 2),
  x = c(1, 1, 2, 1)
) \%>\% group_by(g)
df \%>\% distinct(x)

}
