Created
August 22, 2023 19:08
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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