Sneha Sneha - 3 years ago 71
R Question

Obtain count of unique combination of columns in R dataframe without eliminating the duplicate columns from the data

I have the following data :

A B C D E
1 130 288 6 80 57299
2 288 130 6 57299 80
3 288 130 6 57299 80
4 288 130 6 57299 80
5 288 130 6 57299 80
6 288 130 6 57299 80
7 288 130 6 57299 80
8 288 130 6 57299 80
9 288 130 6 57299 80
10 130 288 6 80 57299


I want to obtain count of unique combination of these columns and append a Frequency column to the existing dataframe without eliminating the duplicate rows. Following is what I want

A B C D E Freq
1 130 288 6 80 57299 2
2 288 130 6 57299 80 8
3 288 130 6 57299 80 8
4 288 130 6 57299 80 8
5 288 130 6 57299 80 8
6 288 130 6 57299 80 8
7 288 130 6 57299 80 8
8 288 130 6 57299 80 8
9 288 130 6 57299 80 8
10 130 288 6 80 57299 2


Trying
df_0 <- count(df, A,B,C,D,E) %>% ungroup()
gives me

A B C D E Freq
1 130 288 6 80 57299 2
2 288 130 6 57299 80 8


By eliminating the duplicates.

How do I go about this?

Wen Wen
Answer Source

using dplyr mutate

dat%>%group_by_(.dots=names(dat))%>%dplyr::mutate(Freq=n())

and Python method

df['Freq']=df.groupby(list(df))['A'].transform('count')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download