I'm trying to sum the values of a numeric column for each level of a factor, but also get the total of all levels in the resulting data frame.
for example:
# Type gender population
# A male 100
# B male 150
# A female 125
# B female 175
using the aggregate function I can get:
aggregate(population ~ gender, df, sum)
# gender population
# male 250
# female 300
but is there a way to get an output that also sums both levels?
# gender population
# all 550
# male 250
# female 300
this can be done easily in SAS with proc tabulate, hopefully there is a way to do it with R also.
thanks in advance,
EDIT
both answers given do work, but I'm trying to find a solution that is less ad-hoc. I'm looking for something that will work on multiple variables, for instance an output like this for a more complex data frame:
# Type gender population
# all all 500
# all male 200
# all female 300
# A all 250
# A male 100
# A female 150
# B all 250
# B male 100
# B female 150
my apologies if that was not clear enough.