La Machine Infernale La Machine Infernale - 1 year ago 106
R Question

R: append column in a dataframe with frequency count based on two columns

I have a dataset like that:

name height
John 188
John 190
Jack 182
Jack 174
Jack 174

And I need to append a column with the frequency count based on both (name) and (height), without modifying the structure of my dataset, e.g. like so:

name height occurrence
John 188 1
John 190 1
Jack 182 1
Jack 174 2
Jack 174 2

Is there a one liner to do so? I've looked around but don't seem to find an answer to that.
Thank you

Answer Source

Using data.table you can use (Thanks @David Arenburg for the improvement)

setDT(df1)[, occurrence := .N, by = .(name, height)]

My original answer (which copies the data insted of adding it by reference via :=)

setDT(df1)[,rep(.N,.N),.(name, height)]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download