2

Here is my data which i want to calculate with:

   data
   type   time grade  money
1     A 1.0000    16   2 
2     A 1.5625    25   3
3     A 4.0000    64   4
4     A 1.0000    16   6
5     A 1.0000    16   3
6     A 1.0000    16   1
7     A 3.0000    48   2
8     B 4.0000    64   8
9     B 4.0000    64   4
10    B 3.0000    48   4
11    C 4.0000    64   3
12    C 1.5625    25   3

i can sum it according to different type with the command,

  sqldf("select type,sum(time),sum(grade) from data group by type")

and get the right result :

I have a try at ggregate(data,by=list("type"),FUN=sum),but get the wrong result.
how can i get it with aggregate?

  type sum(time) sum(grade)
1    A   12.5625        201
2    B   11.0000        176
3    C    5.5625         89
showkey
  • 482
  • 42
  • 140
  • 295

1 Answers1

4

Try

> aggregate(.~type, FUN=sum, data=data)
  type    time grade
1    A 12.5625   201
2    B 11.0000   176
3    C  5.5625    89

In this post you can find some others alternatives.

Community
  • 1
  • 1
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
  • if i add a column `money`, and i do not want to get the colume `money` in my result ,how can i do?how to revise the `aggregate(.~type, FUN=sum, data=data)` – showkey Oct 14 '13 at 11:58
  • i get it `aggregate(cbind(time,money)~type, FUN=sum, data=data) ` – showkey Oct 14 '13 at 12:12
  • @it_is_a_literature you can also do it by subseting the data in the `data` argument as in `aggregate(.~type, FUN=sum, data=data[,c("time","money","type")])`. – Jilber Urbina Oct 18 '13 at 09:36