0

What i'm trying to do is sum a table variable if another one is true, example:

This is a part of my table:

  School  sex  age  studytime
1 2       m    18   2,1
1 1       m    19   2,8
1 2       f    18   2,6
1 3       m    17   2,7
1 3       f    17   1,1
1 4       f    19   3,8
1 1       m    20   2,5

And i need to sum studytime for each sex, something like this:

sex  studytime
m    10,1
f    7,5
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

1

A base R method based on aggregate will look like below:

aggregate(x$studytime,by=list(sex=x$sex),sum)
Chad Y. M
  • 21
  • 4
  • 2
    I think the column is already in numeric, but I could be wrong. Some countries use `,` for decimal signs – Rich Scriven Sep 30 '15 at 01:57
  • Thanks very much @RichardScriven. Just found that nearly half of the world use comma as decimal sign. It's quite incredible I haven't been aware of this until now! – Chad Y. M Sep 30 '15 at 02:09