I have a problem and I need your help please.
I try to sum by group in r without success. I don't understand what I am doing wrong. I have a grouped data frame.
head(my_data_frame)
Source: local data frame [6 x 329]
Groups: x, y [2]
Here an example of data frame
x y a
(int) (int) (dbl)
1 10101101 11 1
2 10101101 11 1
3 10101101 11 0
4 10101101 11 0
5 10101101 12 1
6 10101101 12 1
7 10101101 12 1
8 10101101 21 1
9 10101101 21 0
10 10101101 21 0
I need sum 'a' over 'x' and 'y' to obtain 'b' here is the code that I used but without success
test_df <- my_data_frame %>%
group_by(x, y) %>%
mutate(b = sum(a))
Here is and example of what I need (but the code above don't work in the way that I need)
x y a b
(int) (int) (dbl) (dbl)
1 10101101 11 1 2
2 10101101 11 1 2
3 10101101 11 0 2
4 10101101 11 0 2
5 10101101 12 1 3
6 10101101 12 1 3
7 10101101 12 1 3
8 10101101 21 1 1
9 10101101 21 0 1
10 10101101 21 0 1
Thanks!!