How to do a grouped weighted Median?
Hi, i'm new to using R and I'm trying to find a grouped weighted median. It is simple to get the grouped weighted mean, but i can't seem to find a workable function using google to do this.
The data looks something like the following
| Event | ID | Volume |
|---|---|---|
| 1 | 1.1 | 50 |
| 1 | 1.2 | 10 |
| 1 | 1.3 | 10 |
| 1 | 1.4 | 10 |
| 1 | 1.5 | 10 |
| 2 | 1.1 | 10 |
| 2 | 1.2 | 10 |
| 2 | 1.3 | 30 |
| 2 | 1.7 | 20 |
| 2 | 1.9 | 30 |
| 3 | 1.4 | 20 |
| 3 | 1.5 | 10 |
| 3 | 1.6 | 10 |
| 3 | 1.7 | 10 |
| 3 | 1.8 | 5 |
and the result i'd want returned is
| Event | MedianID |
|---|---|
| 1 | 1.1 |
| 2 | 1.5 |
| 3 | 1.5 |
Can anyone help with this?
Posted Sep 4, 2016, 9:15 AM UTC
310
I imported your data in a data.table (Data). This line of code would give you the result:
That is a very inefficient implementation
What makes this inefficient?
It doesn't scale well with arbitrarily large Volume and group size. You risk memory overflow.
Gotcha, thanks. What would a better implementation look like?