2016-11-09 4 views
2

Bash Gurus, ich brauche die max und Prozentzahlen für jedes Element in der Liste zu berechnen, awk mitCompute Perzentil und Maximalwert pro Variable

aa 1 
ab 3 
aa 4 
ac 5 
aa 3 
ad 2 
ab 4 
ac 2 
ae 2 
ac 5 

Erwartete Ausgabe

Item 90th percentile max value 
aa  3.8    4 
ab  3.9    4 
ac  5    5 
ad  2    2 
ae  2    2 

Am Lage Holen Sie sich die Summe und max mit dem unten, aber nicht das Perzentil.

awk '{ 
item[$1]++; 
count[$1]+=$2; 
max[$1]=$2; 
percentile[$1,.9]=$2 
} 
END{ 
for (var in item) 
print var,count[var],max[var],percentile[var] 
} 
' 

Bitte schlagen Sie vor.

+1

Was erwarten Sie 'Perzentil [$ 1, 0,9] = $ 2' zu tun? –

+3

Mit welcher Methode berechnen Sie das Perzentil? Lineare Interpolation ? Nächster Rang? Haben Sie in bash eine Funktion implementiert? – Aserre

+0

@jas sie sind 1, 3 und 4. – dood

Antwort

2

Perzentilberechnung von Statistics for Dummies 2nd ed. :). In Gnu awk:

$ cat mnp.awk 
BEGIN { 
    PROCINFO["sorted_in"]="@ind_num_asc" # for order in output 
    if(p=="")        # if p not defined it's median 
     p=0.5 
    else 
     p=p/100       # if 90th percentile: p=0.9 
} 
{ 
    v[$1][NR]=$2       # values stored per keyword. NR for unique 
    if($2>m[$1])       # find max val 
     m[$1]=$2 
} 
END { 
    for(i in v) {       # for all keywords 
     n=asort(v[i])      # sort values, n is count 
     prc=p*n;       # percentile figuration 
     if(prc==int(prc)) 
      w=(v[i][prc]+v[i][prc+1])/2 
     else 
      w=v[i][int(prc)+1] 
     print i, m[i], w     # print keyword, max and nth value 
    } 
} 

Run it:

$ awk -p=90 -f mnp.awk data.txt 
aa 4 4 
ab 4 4 
ac 5 5 
ad 2 2 
ae 2 2 

ERLEDIGEN: wenn die Datendatei sortiert wurde, diese rationalisiert werden könnte und nicht alle Daten würden im Speicher abgelegt werden müssen. Hier

+0

Dies ist nicht die erwartete Ausgabe OP geschrieben – dood

+1

@dood Ja, ich wünschte OP hätte die Definition von Perzentil angegeben, die er gewollt hätte. Wikipedia-Seite auf Perzentil zitieren: Es gibt keine Standarddefinition von Perzentil, aber alle Definitionen liefern ähnliche Ergebnisse, wenn die Anzahl der Beobachtungen sehr groß ist. Die Definition, die ich verwendete, stammte aus _Statistics for Dummies_ 2nd ed. –

0

ist eine elegante Lösung, die ich gefunden um das Internet floating den maximalen Wert für die Suche:

{ 
    max[$1] = !($1 in max) ? $2 : ($2 > max[$1]) ? $2 : max[$1] 
} 
END { 
    for (i in max) 
    print i, max[i] 
} 

Ausgang:

ab 4 
ac 5 
ad 2 
ae 2 
aa 4 
+1

Sie würden einfach 'max [$ 1] = (($ 1 in max) && (max [$ 1]> $ 2)) max [$ 1]: $ 2)' um negative ('!') Und wiederholte (Einstellung auf '$ 2' an mehreren Stellen) Syntax. –

0

datamash ist ein schönes Werkzeug, obwohl es nicht das nicht unterstützt Perkantilteil.

$ datamash -W --sort --group=1 max 2 min 2 < INPUT 
aa 4 1 
ab 4 3 
ac 5 2 
ad 2 2 
ae 2 2 

Es unterstützt die folgenden Operationen

File operations: 
    transpose, reverse 
Numeric Grouping operations: 
    sum, min, max, absmin, absmax 
Textual/Numeric Grouping operations: 
    count, first, last, rand 
    unique, collapse, countunique 
Statistical Grouping operations: 
    mean, median, q1, q3, iqr, mode, antimode 
    pstdev, sstdev, pvar, svar, mad, madraw 
    pskew, sskew, pkurt, skurt, dpo, jarque