-2

I have a dataframe that looks like the following:

df<-data.frame(group=c("A","A","B"))

What's the simplest way of adding another column to this that has the count of each occurrence of the column group?

The result should look like the following:

  group count
1     A     2
2     A     2
3     B     1
locket
  • 729
  • 2
  • 13
  • 3
    https://stackoverflow.com/questions/7450600/count-number-of-rows-per-group-and-add-result-to-original-data-frame – user63230 Jul 27 '22 at 11:26

1 Answers1

-1
library(dplyr)

df %>% 
  group_by(group) %>% 
  mutate(count = n())
Julian
  • 6,586
  • 2
  • 9
  • 33