Obtain count of unique combination of columns in R dataframe without eliminating the duplicate columns from the data -
i have following data :
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 obtain count of unique combination of these columns , append frequency column existing dataframe without eliminating duplicate rows. following want
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
b c d e freq 1 130 288 6 80 57299 2 2 288 130 6 57299 80 8
by eliminating duplicates.
how go this?
r dplyr
mutate
dat%>%group_by_(.dots=names(dat))%>%dplyr::mutate(freq=n())
python transform
df['freq']=df.groupby(list(df))['a'].transform('count')
Comments
Post a Comment