a <- c(rep(1:2,3))
b <- c("A","A","B","B","B","B")
df <- data.frame(a,b)
> str(b)
chr [1:6] "A" "A" "B" "B" "B" "B"
a b
1 1 A
2 2 A
3 1 B
4 2 B
5 1 B
6 2 B
I want to group by variable a
and return the most frequent value of b
My desired result would look like
a b
1 1 B
2 2 B
In dplyr
it would be something like
df %>% group_by(a) %>% summarize (b = most.frequent(b))
I mentioned dplyr
only to visualize the problem.
df %>% group_by(a) %>% summarize (b =names(which.max(table(b))))
– Paretomax(table(b))
. Full code would be:df %>% group_by(a) %>% summarize (b =names(which.max(table(b))), count_b = max(table(b)))
– Moriarty