The sample data is as below:
n | period | age |
---|---|---|
15 | 1991 | 5 |
20 | 1991 | 5 |
16 | 1991 | 15 |
29 | 1991 | 15 |
77 | 1991 | 25 |
44 | 1991 | 25 |
I use the following code to get the sum from the data grouped by period
and age
:
#The name of dataset is a.
a %>% group_by(period,age)%>%
mutate(n = sum(n))
But the result is:
n | period | age |
---|---|---|
35 | 1991 | 5 |
35 | 1991 | 5 |
45 | 1991 | 15 |
45 | 1991 | 15 |
121 | 1991 | 25 |
121 | 1991 | 25 |
Why there is duplicate rows? It is because it sums every element in each groups?