Aggregation of data frames by the sum

Say I have two data frames:

d1 = data.frame(table(c(1,2,2,2,4,5))) d2 = data.frame(table(c(2,2,2,6))) 

Frame compression with rbind yields the following:

 > rbind(d1, d2) Var1 Freq 1 1 1 2 2 3 3 4 1 4 5 1 5 2 3 6 5 1 

But I would like to calculate the sum of Freq values ​​with the same Var1 , i.e. get

  Var1 Freq 1 1 1 2 2 6 3 4 1 4 5 1 5 6 1 

How can i do this?

+4
source share
1 answer

In addition to aggregate there is also xtabs , which is designed specifically to summarize tables.

 xtabs(Freq ~ Var1, data=rbind(d1, d2)) 
+4
source

Source: https://habr.com/ru/post/1489272/


All Articles