R:不同级别的总和

Har*_*had 1 aggregate r sum levels

我有一个数据集X:

customer_id event_type tot_count
931 1 5
231 2 6
231 1 3
333 3 9
444 1 1
931 3 3
333 1 21
444 2 43
Run Code Online (Sandbox Code Playgroud)

我需要一个总和customer_id和event_type水平.这是SQL中的1行代码:

select customer_id, event_type, sum(tot_count) from X group by 1,2
Run Code Online (Sandbox Code Playgroud)

我需要在R中进行相同的操作

Sve*_*ein 5

你可以使用这个aggregate功能:

aggregate(tot_count ~ customer_id + event_type, X, sum)

 customer_id event_type tot_count
1         231          1         3
2         333          1        21
3         444          1         1
4         931          1         5
5         231          2         6
6         444          2        43
7         333          3         9
8         931          3         3
Run Code Online (Sandbox Code Playgroud)