I would like to understand when and how the .by
will work in mutate
, I tried to use the .by
within mutate
in the example provided
tribble(
~ID, ~Country, ~Sales,
1, 'Austria', 6,
1, 'Austria', 6,
1, 'Belgium', 6,
2, 'Belgium', 10,
2, 'Czech', 10,
3, 'Denmark', 3,
3, 'Germany', 3) %>%
mutate(n=n(), .by=ID)
# output
# A tibble: 7 × 5
ID Country Sales n .by
<dbl> <chr> <dbl> <int> <dbl>
1 1 Austria 6 7 1
2 1 Austria 6 7 1
3 1 Belgium 6 7 1
4 2 Belgium 10 7 2
5 2 Czech 10 7 2
6 3 Denmark 3 7 3
7 3 Germany 3 7 3
However in the example below if i use the group_by
we get a different output which is the expected output
tribble(
~ID, ~Country, ~Sales,
1, 'Austria', 6,
1, 'Austria', 6,
1, 'Belgium', 6,
2, 'Belgium', 10,
2, 'Czech', 10,
3, 'Denmark', 3,
3, 'Germany', 3) %>%
group_by(ID) %>% mutate(n=n())
# output
# A tibble: 7 × 4
# Groups: ID [3]
ID Country Sales n
<dbl> <chr> <dbl> <int>
1 1 Austria 6 3
2 1 Austria 6 3
3 1 Belgium 6 3
4 2 Belgium 10 2
5 2 Czech 10 2
6 3 Denmark 3 2
7 3 Germany 3 2