I assumed there could be two kinds of gear
for a given ss
. In that case the problem boils down to finding the median (or mode) of a character variable. Here is code to find the mode of a character variable (here gear
).
catch <- read.table(text = '
ss gear
1 AA
1 AA
1 BB
1 BB
2 CC
2 CC
2 CC
3 BB
4 AA
4 CC
', header = TRUE)
gear.mode <- tapply(catch$gear, catch$ss, function(x) { y = table(x) ; names(y)[y==max(y)] })
gear.mode <- as.data.frame(gear.mode)
gear.mode
gear.mode
1 AA, BB
2 CC
3 BB
4 AA, CC
You can also do this with aggregate
:
aggregate(gear ~ ss, data = catch, FUN = function (x) {
y = table(x) ; names(y)[y==max(y)]
})
ss gear
1 1 AA, BB
2 2 CC
3 3 BB
4 4 AA, CC