This task seems very simple and yet I still can't find an answer. I've seen solutions for weighted averages (i.e. means) in pivot tables but not weighted medians.
I need to create a pivot table with weighted medians (using two columns, one with the value and one with the weight–the quantity of each value to be calculated towards the median). In other words…
city value weight
Phoenix 100 40
Phoenix 140 12
Phoenix 500 15
San Francisco 700 5
San Francisco 300 5
San Francisco 350 1
San Francisco 600 1
will return…
city weighted median
Phoenix 100
San Francisco 475
I've been trying to find an adequate solution in PostgreSQL and R but so far no luck. Any help appreciated!