我在R中有一个数据框,其中有很多重复的记录。我有兴趣找出该数据框中每个记录有多少条记录。
例如,我有以下数据框:
Fake Name Fake ID Fake Status Fake Program
June 0003 Green PR1
June 0003 Green PR1
Television 202 Blue PR3
Television 202 Green PR3
Television 202 Green PR3
CRT 12 Red PR0
从上面我想得到类似下面的东西:
Fake Name Fake ID Fake Status Fake Program COUNT
June 0003 Green PR1 2
Television 202 Blue PR3 1
Television 202 Green PR3 2
CRT 12 Red PR0 1
任何帮助,将不胜感激。谢谢你。
最佳答案
使用group_by_all
,然后使用n
计算行数:
df %>% group_by_all() %>% summarise(COUNT = n())
# A tibble: 4 x 5
# Groups: Fake.Name, Fake.ID, Fake.Status [?]
# Fake.Name Fake.ID Fake.Status Fake.Program COUNT
# <fct> <int> <fct> <fct> <int>
#1 CRT 12 Red PR0 1
#2 June 3 Green PR1 2
#3 Television 202 Blue PR3 1
#4 Television 202 Green PR3 2
甚至比@Ryan的评论更好:
df %>% group_by_all %>% count