using functions to compute aggregate tables

better than sourcing R scripts for the sake of memory