vectorbase::FrequencyCount

Description

Compute the frequency count of elements in a vector.

Syntax

int FrequencyCount( double dMin, double dMax, double dInc, vector<int> & vCounts, int wIncludeOutliers = FAB_NOT_INCLUDE_OUTLIERS )

Parameters

dMin
[input] bin starting value
dMax
[input] bin end value
dInc
[input] bin size
vCounts
[modify] vector containing frequency counts
wIncludeOutliers
[input] Flag indicating whether or not to include outliers in the first and last bins. Default is FAB_NOT_INCLUDE_OUTLIERS

Return

0, success

1, dInc is zero

2, dInc is greater than (dMax - dMin)

Examples

EX1

void vectorbase_FrequencyCount_ex1()
{
    // Declare a vector and fill with some data
    vector vecData = {1, 2, 1, 1, 2, 2, 2, 3, 3, 4, 5};
    
    // Declare vector to hold frequency count
    vector<int> vecCount;
    
    // Get frequency count for vecData into vecCount
    // Note that bin end value is set to 6, which is larger
    // than the max value in vecData
    vecData.FrequencyCount(1, 6, 1, vecCount);
    for (int ii = 0; ii < vecCount.GetSize(); ii++)
        out_int("",vecCount[ii]);
    // Result:
    //        vecCount = {3, 4, 2, 1, 1}
}

EX2

void vectorbase_FrequencyCount_ex2()
{
    // This function will create a new worksheet, add a column
    // with some random data, then compute frequency count and place
    // in adjacent columns.
    
    // Create a new worksheet
    Worksheet wks;
    wks.Create();
    
    // Delete all existing columns
    while(wks.DeleteCol(0));
    
    // Add a new column and fill with 100 random numbers
    int index;
    index = wks.AddCol("RndData");
    Dataset dsData(wks, index);
    dsData.SetSize(100);
    for( int ii = 0; ii < 100; ii++ )
        dsData[ii] = ran();
    
    // Compute frequency count from 0 to 1 with binsize of 0.1
    vector<int> vecFreqCount;
    double dBinBegin = 0.0;
    double dBinEnd = 1.0;
    double dBinSize = 0.1;
    int iRet = dsData.FrequencyCount(dBinBegin, dBinEnd, dBinSize, vecFreqCount);
    if(0 == iRet)
    {
        // Add column for bin center and set as type X and fill column
        index = wks.AddCol("BinCenter");
        wks.Columns(index).SetType(OKDATAOBJ_DESIGNATION_X);
        int iNumBins = (dBinEnd - dBinBegin) / dBinSize;
        Dataset dsBinCenter( wks, index );
        dsBinCenter.SetSize( iNumBins );
        for( int ii = 0; ii < iNumBins; ii++ )
            dsBinCenter[ii] = dBinBegin + (0.5 + ii) * dBinSize;
        
        // Add column for bin counts and fill column
        index = wks.AddCol("BinCounts");
        Dataset dsBinCounts(wks, index);
        dsBinCounts = vecFreqCount;                    
    }
    else
        out_str("Failed to compute frequency count"); 
}

Remark

Compute the frequency count of elements in the vector, with specified bin starting value, bin end value, and bin size. Values that fall on the lower edge of a bin are included in that bin and values that fall on the upper edge of a bin are included in the next higher bin.

See Also

header to Include

origin.h