I am trying to find the most frequent value by group. In the following dataframe example:
df<-data.frame(a=c(1,1,1,1,2,2,2,3,3),b=c(2,2,1,2,3,3,1,1,2)) > df ab 1 1 2 2 1 2 3 1 1 4 1 2 5 2 3 6 2 3 7 2 1 8 3 1 9 3 2
I would like to add a column βcβ that has the most counter meaning in βbβ when its values ββare grouped by βaβ. I need the following output:
> df abc 1 1 2 2 2 1 2 2 3 1 1 2 4 1 2 2 5 2 3 3 6 2 3 3 7 2 1 3 8 3 1 1 9 3 2 1
I tried using the table and clicking, but did not understand. Is there a quick way to do this?
Thank you
source share