by()
each value of a
, create a table()
of b
and extract the names()
of the largest entry in that table()
:
> with(df,by(b,a,function(xx)names(which.max(table(xx)))))
a: 1
[1] "B"
------------------------
a: 2
[1] "B"
You can wrap this in as.table()
to get a prettier output, although it still does not exactly match your desired result:
> as.table(with(df,by(b,a,function(xx)names(which.max(table(xx))))))
a
1 2
B B