why the .by does not work in mutate in the example provided

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

I just ran the first code chunk you provided and got the expected result. As a check, I'm using dplyr version 1.1.2.

> 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)
# A tibble: 7 x 4
     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

Thank you @ttrodrigz , i checked the dplyr version to be 1.0.10, i updated the version and it works.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.