0

I have a data set that looks like this:

    Num  Flavor
     1   Chocolate
     2   Chocolate
     3   Chocolate
     4   Strawberry
     5   Strawberry
     6   Vanilla
     7   Chocolate

My goal is to create a column which counts the number of times each unique value is present in the data, next to the variable which it is counting, like this:

Num Cnt Flavor
 1   1  Chocolate
 2   2  Chocolate
 3   3  Chocolate
 4   1  Strawberry
 5   2  Strawberry
 6   1  Vanilla
 7   4  Chocolate

And so forth.

knaslund
  • 33
  • 4

1 Answers1

1

You could use arrange and mutate to solve this. Not sure if this is the easiest way to go..

library("tidyverse")
df %>%
  arrange(Flavor) %>%
  group_by(Flavor) %>%
  mutate(Cnt = 1:length(Flavor)) %>%
  arrange(Num)


# A tibble: 7 x 3
# Groups:   Flavor [3]
    Num Flavor       Cnt
  <int> <fct>      <int>
1     1 Chocolate      1
2     2 Chocolate      2
3     3 Chocolate      3
4     4 Strawberry     1
5     5 Strawberry     2
6     6 Vanilla        1
7     7 Chocolate      4
nadizan
  • 1,323
  • 10
  • 23
  • You don't need `arrange` to obtain the same output. `dt %>% group_by(Flavor) %>% mutate(Cnt = seq_along(Flavor))` returns the same result. – utubun Oct 30 '18 at 15:11
  • Your code does not work. – nadizan Oct 30 '18 at 15:45
  • What exactly is wrong with it? Works fine for me. Even shorter, withing `mutate` you can do `... %>% mutate(Cnt = 1:n())` – utubun Oct 30 '18 at 16:31