Postgresql Array Agg Group By Function In R