1

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!!

2 Answers2

0

aggregate should easily solve this:

merge(my_data_frame, setNames(aggregate(a ~ x + y, data = my_data_frame, sum), c('x', 'y', 'b')), all.x = TRUE)
SeaSprite
  • 564
  • 8
  • 12
0

The mutate is one of the function that is found in more than one package i.e. in plyr and dplyr. So, if both packages are loaded, there is a chance that mutate from dplyr got masked by the plyr version of mutate. In order to rectify it, we can use dplyr::mutate

test_df <- my_data_frame %>%
                  group_by(x, y) %>%
                  dplyr::mutate(b = sum(a))
akrun
  • 874,273
  • 37
  • 540
  • 662