typestar

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

  1. distinct on a subset of columns keeps the first row of each.
  2. .keep_all retains the other columns.
  3. count with a filter is how you find duplicates.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 5 in Rows, step 7 of 27 in dplyr.

← Previous Next →