Distinct and duplicates in R
Dropping repeated rows, and finding the ones that repeat.
library(dplyr)
runs <- tibble(
lang = c("r", "r", "python", "python", "python"),
day = c(1, 1, 2, 3, 3),
tpm = c(88, 88, 104, 99, 101)
)
print(distinct(runs))
print(distinct(runs, lang))
print(distinct(runs, lang, .keep_all = TRUE))
print(filter(count(runs, lang, day), n > 1))
print(n_distinct(runs$lang), nrow(runs))
How it works
distincton a subset of columns keeps the first row of each..keep_allretains the other columns.countwith a filter is how you find duplicates.
Keywords and builtins used here
ccountdistinctfilterlibraryn_distinctnrowprinttibble
The run, in numbers
- Lines
- 13
- Characters to type
- 320
- Tokens
- 119
- Three-star pace
- 95 tpm
At the three-star pace of 95 tokens a minute, this run takes about 75 seconds.
Step 3 of 5 in Rows, step 7 of 27 in dplyr.