2.2.3.1.3 CategoricalData::Group

Description

Sort an input vector into columns in a matrix by this CategoricalData set.

Syntax

int Group( vector & vIn, matrix & mOut, vector<int> & vColumnSize )

Parameters

vIn
[input]The input data to be sorted into groups by this CategoricalData set
mOut
[output]The output matrix sorted into groups
vColumnSize
[output] Vector containing the number of data points in each group (column of the matrix)

Return

Returns 0 on success and a non-zero error code on failure.

Examples

EX1

void CategoricalData_Group_ex1()
{
    // worksheet's 1st and 2nd column should have following data
    // A(X) = {Tic, Tic, Tac, Toe, Tic, Toe, Tac}
    // B(Y) = {1,2,3,4,5,6,7};

    Worksheet wks;
    wks.Create();
    if (wks.IsValid())
    {     
        Dataset ds1(wks,0);
        Dataset ds2(wks,1);
        vector v2 = {1,2,3,4,5,6,7};
        vector<string> vs = {"Tic", "Tic", "Tac", "Toe", "Tic", "Toe", "Tac"};
        
        ds1.PutStringArray(vs);
        ds2 =  v2;
        string strWksColAname = wks.Columns(0).GetName();
        strWksColAname = wks.GetPage().GetName() + "_" + strWksColAname;
        
        string strWksColBname = wks.Columns(1).GetName();
        strWksColBname = wks.GetPage().GetName() + "_" + strWksColBname;
        
        CategoricalData cdMyData(strWksColAname);
        Dataset dsIn(strWksColBname);
        
        MatrixPage mpg ;//= Project.MatrixPages(0);
        mpg.Create("Origin");
        if (mpg.IsValid())
        {
            Matrix mOut(mpg.GetName());
            vector<int> vCounts;
            int ii;
            StringArray vMyMap;
            cdMyData.Group( dsIn, mOut, vCounts );
            vMyMap = (StringArray) cdMyData.Map;
            for( ii = 0; ii < vCounts.GetSize(); ii++ )
                printf("Column %d ('%s' group) has %d values\n", ii+1, vMyMap[ii], vCounts[ii] );
        }
    }
}

Remark

Sort an input vector into columns in a matrix by this CategoricalData data set. Each value in the input vector is sorted into the column of the matrix having the 1 based column number equal to its paired index value in this CategoricalData data set. The input vector and this CategoricalData data set must have the same number of elements.

The matrix will be dynamically sized having the same number of columns as there are unique index values in the CategoricalData data set and having as many rows as are necessary to hold the largest group of sorted values. An Output vector gives the number of grouped values in each column. This method is currently implemented only for input vectors having an underlying base type of double.

See Also

Header to Include

origin.h