Wednesday, December 20, 2006

Calculating the median of a set of values

Doing an average on data isn't always the best way to deal with a set of data that you want to query.

The median, the value sitting in the middle where 50% of the values are lower and 50% of the values are higher, is a good measure.

For example, the median of 10,20,30 is the middle value of 20. In this case, 20 would also be the average (10+20+30=60,60/3=20). The median of 10,20,300 is still 20, but the average in this case would be 110 (10+20+300=330,330/3=110).

Script #1:
Calculate median of a file provided as an argument. Input file must be formatted as one numerical entry per line, formatted as something like:
20
30
40
70

Output is simply the median of the entire set of values.

get-median.ps1: get-median.ps1

Script #2:
Calculate median of a input file provided as an argument. Input file must be formatted as one entry per line, formatted as something like:
User1,20
User1,30
User2,40
User2,70

Output is the median of each set of users. For example:
User1,20.5
User2,55

get-median2.ps1: get-median2.ps1