Identifies rows with duplicate values in the specified key columns. Returns a data frame containing only the rows with duplicated keys, along with a count of occurrences.
Usage
key_duplicates(data, by, keep = c("all", "first", "last"))Value
A data frame containing the duplicated rows, with an additional
column .n_duplicates showing how many times each key appears.
Returns an empty data frame (0 rows) if no duplicates found.
Examples
df <- data.frame(
id = c(1, 2, 2, 3, 3, 3, 4),
value = letters[1:7]
)
# Find all duplicates
key_duplicates(df, by = "id")
# Find first occurrence only
key_duplicates(df, by = "id", keep = "first")