Skip to content

Instantly share code, notes, and snippets.

@matt-dray
Created August 22, 2023 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matt-dray/bc8e7edc5e8d3e4672ac77349d0b37f3 to your computer and use it in GitHub Desktop.
Save matt-dray/bc8e7edc5e8d3e4672ac77349d0b37f3 to your computer and use it in GitHub Desktop.
Count the number of times that dulicate records appear in a dataframe
x <- data.frame(
col_a = c("A", "B", "A", "C", "A", "D", "B", "C"),
col_b = c(1, 2, 1, 3, 9, 4, 2, 9)
)
x
# col_a col_b
# 1 A 1
# 2 B 2
# 3 A 1
# 4 C 3
# 5 A 9
# 6 D 4
# 7 B 2
# 8 C 9
y <- aggregate(x, by = x, length)[-(ncol(x) + 1)]
names(y)[ncol(y)] <- "frequency"
y
# col_a col_b frequency
# 1 A 1 2
# 2 B 2 2
# 3 C 3 1
# 4 D 4 1
# 5 A 9 1
# 6 C 9 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment