0

So I have lots of categories within one column. Within those categories, I have another category in another column. Variable 1 and 2 is what I have. I want the frequency column added.

Variable 1  ~      Variable 2     ~ Frequency

Category 1 ~       Red    ~        2 (because there are 2 red's in category 1)

~

Category 1   ~     Blue    ~       1

~

Category 1  ~      Red    ~        2 (again, because there are 2 red's in category 1)

~

Category 2   ~     Blue      ~     2 

~

Category 2  ~      Red      ~      1


~

Category 2    ~    Blue    ~       2

I want to add/mutate another column that will tell me what the frequency.

Thank you!

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
jay
  • 47
  • 3
  • You can use `df %>% add_count(Variable1, Variable2)`. See https://stackoverflow.com/questions/7450600/count-number-of-rows-per-group-and-add-result-to-original-data-frame – Ronak Shah Dec 07 '20 at 02:25

1 Answers1

0
library(dplyr)
your_data %>%
  group_by(Variable_1, Variable_2) %>%
  mutate(Frequency = n())
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294