Help with Median


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with Median
# 8  
Old 07-16-2009
Thank you Vgersh99, but I'm afraid it's a bit too complicated for me. Being a beginer, I don't get half of what is written there... Sorry!
# 9  
Old 07-16-2009
[Courtesy of http://dada.perl.it/shootout/gawk_allsrc.html]

nawk -f stats.awk myFile

stats.awk:
Code:
# $Id: moments.gawk,v 1.1 2001/05/13 07:09:06 doug Exp $
# http://www.bagley.org/~doug/shootout/

BEGIN {
    #delete ARGV;
    sum = 0;
    n = 0;
}

{
    nums[n++] = $1;
    sum += $1;
}

END {
    mean = sum/n;
    for (num in nums) {
    dev = nums[num] - mean;
    if (dev > 0) { avg_dev += dev; } else { avg_dev -= dev; }
    vari += dev^2;
    skew += dev^3;
    kurt += dev^4;
    }
    avg_dev /= n;
    vari /= (n - 1);
    std_dev = sqrt(vari);

    if (vari > 0) {
    skew /= (n * vari * std_dev);
    kurt = kurt/(n * vari * vari) - 3.0;
    }

    nums[n] = nums[0];
    heapsort(n, nums);

    mid = int(n/2)+1;
    median = (n % 2) ? nums[mid] : (nums[mid] + nums[mid-1])/2;

    printf("n:                  %d\n", n);
    printf("median:             %f\n", median);
    printf("mean:               %f\n", mean);
    printf("average_deviation:  %f\n", avg_dev);
    printf("standard_deviation: %f\n", std_dev);
    printf("variance:           %f\n", vari);
    printf("skew:               %f\n", skew);
    printf("kurtosis:           %f\n", kurt);
}

function heapsort (n, ra) {
    l = int(0.5+n/2) + 1
    ir = n;
    for (;;) {
        if (l > 1) {
            rra = ra[--l];
        } else {
            rra = ra[ir];
            ra[ir] = ra[1];
            if (--ir == 1) {
                ra[1] = rra;
                return;
            }
        }
        i = l;
        j = l * 2;
        while (j <= ir) {
            if (j < ir && ra[j] < ra[j+1]) { ++j; }
            if (rra < ra[j]) {
                ra[i] = ra[j];
                j += (i = j);
            } else {
                j = ir + 1;
            }
        }
        ra[i] = rra;
    }
}

# 10  
Old 07-16-2009
Yes, thank you! I have seen it through the link you've attached, but I don't understand it. I will look thnigs up, but was hoping there's a simpler way of doing it. Somebody's hinted that I should use awk, but then I saw something on this forum and decided to ask.
Does anybody know anything simpler than this huge script, please?

Many thanks in advance!

---------- Post updated at 09:28 AM ---------- Previous update was at 09:24 AM ----------

I have just tried ot run
nawk -f stats.awk myfile
but it doesn't work. Nawk is not recognized as a command...
# 11  
Old 07-16-2009
sort myDataFile | awk -f median.awk

median.awk:
Code:
{
    nums[++n] = $1;
}

END {

    mid = int(n/2)+1;
    median = (n % 2) ? nums[mid] : (nums[mid] + nums[mid-1])/2;

    printf("median:             %f\n", median);
}



---------- Post updated at 10:38 AM ---------- Previous update was at 10:37 AM ----------

Quote:
Originally Posted by zajtat
Yes, thank you! I have seen it through the link you've attached, but I don't understand it. I will look thnigs up, but was hoping there's a simpler way of doing it. Somebody's hinted that I should use awk, but then I saw something on this forum and decided to ask.
Does anybody know anything simpler than this huge script, please?

Many thanks in advance!

---------- Post updated at 09:28 AM ---------- Previous update was at 09:24 AM ----------

I have just tried ot run
nawk -f stats.awk myfile
but it doesn't work. Nawk is not recognized as a command...
then use either 'awk' or 'gawk'
# 12  
Old 07-16-2009
It does work now! I've got loads to learn and understand now!!!!

Many thanks for your help!!!!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print median values of matrix -awk?

I use the following script to print the sum and how could I extend this to print medians instead? thanks name s1 s2 s3 s4 g1 2 8 6 5 g1 5 7 9 9 g1 6 7 8 9 g2 8 8 8 8 g2 7 7 7 7 g2 10 10 10 10 g3 3 12 1 24 g3 5 5 24 48 g3 12 3 12 12 g3 2 3 3 3 output name s1 s2 s3 s4 g1 5 7 8 9... (5 Replies)
Discussion started by: quincyjones
5 Replies

2. UNIX for Dummies Questions & Answers

Median calculator based on id match

I am trying to calculate the median of a column of numbers if they match an ID type on a different column. The input file has 3 columns. The column that has the ID is column 1 and the column with the values I'd like to find the median for is column 3. The file does not need to be sorted. What I... (9 Replies)
Discussion started by: verse123
9 Replies

3. Shell Programming and Scripting

Median and max of duplicate rows

Hi all, plz help me with this, I want to to extract the duplicate rows (column 1) in a file which at least repeat 4 times. then I want to summarize them by getting the max , mean, median and min. The file is sorted by column 1, all the repeated rows appear together. If number of elements is... (5 Replies)
Discussion started by: ritakadm
5 Replies

4. Shell Programming and Scripting

Awk based script to find the median of all individual columns in a data file

Hi All, I have some data like below. Step1,Param1,Param2,Param3 1,2,3,4 2,3,4,5 2,4,5,6 3,0,1,2 3,0,0,0 3,2,1,3 ........ so on Where I need to find the median(arithmetic) of each column from Param1...to..Param3 for each set of Step1 values. (Sort each specific column, if the... (5 Replies)
Discussion started by: ks_reddy
5 Replies

5. Shell Programming and Scripting

Compute the median of a set of numbers with AWK?

Is there a way in awk to compute the median of a set of numbers in a file in the following format. 34 67 78 100 23 45 67 (3 Replies)
Discussion started by: Lucky Ali
3 Replies

6. Shell Programming and Scripting

awk to median

hi! i have a file like the attachement. you can see on the last column, there is a marker from 1 to 64 for each time. I'd like to have the median for each marker: i want to get a median every 128 values the result is : for an hour and marker x, i have the median value thank you for... (5 Replies)
Discussion started by: riderman
5 Replies
Login or Register to Ask a Question