我有一个大数据帧(698764 X 9),其格式类似于:
df <- data.frame(id = c(1, 2, 1, 4), units = c(2, 2, 2, 5), region = c("US", "CA", "US", "IN))
我们可以看到第一行和第三行是完全相同的。我想提取重复的行然后计算它在数据中重复了多少次这样输出看起来就像
duplicates <- data.frame(id = 1, units = 2, region = "US", times = 2)
,"times"是该行重复的次数。
我使用
提取重复的行new_df <- df[duplicated(df),]
但我不确定如何计算出现的次数。
我们可以用count
library(dplyr)
df %>%
count(id, units, region, name = 'times')
与产出
id units region times
1 1 2 US 2
2 2 2 CA 1
3 4 5 IN 1
或使用
df %>%
count(across(everything()), name = 'times')
id units region times
1 1 2 US 2
2 2 2 CA 1
3 4 5 IN 1