3.4 mavg - Calculate Average

Calculates the average values in column specified by the f= parameter.

Format

mavg f= [k=] [-n] [i=] [o=] [-nfn] [-nfno] [-x] [-q] [precision=] [--help] [--version]

Parameters

f=

Specify the field(s) (multiple fields can be specified) with the values to be aggregated.

 

Use : (colon) to specify the new field name. Example: f=Quantity:AverageQuantity

k=

Specify the set of field name(s) (Multiple fields can be specified) as unit of aggregation.

-n

Output as NULL if the data consist at least one NULL value.

Examples

Example 1: Basic Example

Calculate the average values of "Quantity" and "Amount" fields for each "Customer", save the computed output in new columns named "AverageVolume" and "AverageAmount".

$ more dat1.csv
Customer,Quantity,Amount
A,1,5
A,2,20
B,1,15
B,,10
B,5,20
$ mavg k=Customer f=Quantity:AvgQuantity,Amount:AvgAmount i=dat1.csv o=rsl1.csv
#END# kgavg f=Quantity:AvgQuantity,Amount:AvgAmount i=dat1.csv k=Customer o=rsl1.csv
$ more rsl1.csv
Customer%0,AvgQuantity,AvgAmount
A,1.5,12.5
B,3,15

Example 2: Output consisting of NULL values

Calculate the average values of "Quantity" and "Amount" fields for each "Customer", save output in a new columns named "AverageVolume" and "AverageAmount". When specifying the -n option, if a NULL value is included in the input, the result will return NULL value.

$ mavg k=Customer f=Quantity:AvgQuantity,Amount:AvgAmount -n i=dat1.csv o=rsl2.csv
#END# kgavg -n f=Quantity:AvgQuantity,Amount:AvgAmount i=dat1.csv k=Customer o=rsl2.csv
$ more rsl2.csv
Customer%0,AvgQuantity,AvgAmount
A,1.5,12.5
B,,15

Example 3: Calculate sum without key field

Calculate the average values of "Quantity" and "Amount" fields, and save the outputs in columns "AvgQuantity" and "AvgAmount".

$ mavg f=Quantity:AvgQuantity,Amount:AvgAmount i=dat1.csv o=rsl3.csv
#END# kgavg f=Quantity:AvgQuantity,Amount:AvgAmount i=dat1.csv o=rsl3.csv
$ more rsl3.csv
Customer,AvgQuantity,AvgAmount
B,2.25,14

Related Commands

mhashavg : Aggregate calculation does not require prior sorting on the key field.

msum : Command to calculate sum.

mstats : Calculate a variety of statistics.