0

I would like to add a column to my dataframe which counts the number of observations in another column

Here is an example:

   df <- data.frame(colour=c("blue","blue","blue","green","green","orange","grey"))

And this is the output I am hoping for:

  colour count 
1   blue   3
2   blue   3
3   blue   3
4  green   2
5  green   2
6  orange  1 
7  grey    1

I could do this with a join:

count <- df %>% group_by(colour) %>% tally()
df <- df %>% left_join(count)

But I would rather do it in one statement

Shinobi_Atobe
  • 1,793
  • 1
  • 18
  • 35

0 Answers0