3.9.3.1 Basic Summary Stats
Version Info
Minimum Origin Version Required: Origin 8 SR0
Description
The following examples show how to do statistics on the selected data and the output results. Please refer to ocmath_basic_summary_stats function for more details.
Example
EX1
This example shows how to do basic statistics on a single region based on the minimum and maximum selected
rows and columns. Multiple ranges are ignored:
void stats_on_selected_ranges_ex1()
{
Worksheet wks = Project.ActiveLayer();
if( !wks )
return;
int c1, c2, r1, r2;
int nRet = wks.GetSelection(c1, c2, r1, r2); // get the selected range
if( WKS_SEL_NONE == nRet )
{
out_str("No selection on active worksheet!");
return;
}
DataRange dr;
dr.Add("X", wks, r1, c1, r2, c2); //construct a data range object by selection
vector vData;
dr.GetData(vData, 0); // get data from range object
// minimum and maximum
double dMin, dMax;
vData.GetMinMax(dMin, dMax);
printf("Statistics result of the selected ranges:\n");
printf("Min=%f, Max=%f\n", dMin, dMax);
int nN;
double dMean, dSD, dSum;
nRet = ocmath_basic_summary_stats(vData.GetSize(), vData, &nN, &dMean, &dSD, NULL, NULL, &dSum);
if(0 == nRet)
{
printf("N=%d, Mean=%f, SD=%f, Sum=%f\n", nN, dMean, dSD, dSum);
}
}
EX2
The following example shows how to do statistics on multiple ranges, whether contiguous or not. This example does aggregate statistics for all selected regions.
// Select multiple regions before running this code.
void stats_on_selected_ranges_ex2()
{
Worksheet wks = Project.ActiveLayer();
if( !wks )
return;
//Get selected regions
vector<int> vR1, vC1, vR2, vC2;
int nSelType = wks.GetSelectedRange(vR1, vC1, vR2, vC2);
int nNumRegions = vR1.GetSize(); //vR1 & vC1 & vR2 & vC2 should have same size.
printf("The number of regions you select is : %d\n", nNumRegions);
//add data of each region into vData
vector vData;
for ( int iRegion = 0; iRegion < nNumRegions; iRegion++ )
{
DataRange dr;
dr.Add("X", wks, vR1[iRegion], vC1[iRegion], vR2[iRegion], vC2[iRegion]);
vector vTemp;
if ( dr && dr.GetData(vTemp, 0) )
{
vData.Append(vTemp);
}
}
//Get minimum and maximum
double dMin, dMax;
vData.GetMinMax(dMin, dMax);
printf("Statistics result of the selected ranges:\n");
printf("Min=%f, Max=%f\n", dMin, dMax);
int nN;
double dMean, dSD, dSum;
int nRet = ocmath_basic_summary_stats(vData.GetSize(), vData, &nN, &dMean, &dSD, NULL, NULL, &dSum);
if(0 == nRet)
{
printf("N=%d, Mean=%f, SD=%f, Sum=%f\n", nN, dMean, dSD, dSum);
}
}
EX3
The following example shows how to do statistics on multiple ranges, whether contiguous or not. This example does individual statistics for each selected region.
void stats_on_selected_ranges_ex3()
{
Worksheet wks = Project.ActiveLayer();
if( !wks )
return;
//Get selected regions
vector<int> vR1, vC1, vR2, vC2;
int nSelType = wks.GetSelectedRange(vR1, vC1, vR2, vC2);
int nNumRegions = vR1.GetSize(); //vR1 & vC1 & vR2 & vC2 should have same size.
printf("The number of regions you select is : %d\n", nNumRegions);
vector vData;
for ( int iRegion = 0; iRegion < nNumRegions; iRegion++ )
{
DataRange dr;
dr.Add("X", wks, vR1[iRegion], vC1[iRegion], vR2[iRegion], vC2[iRegion]);
vector vTemp;
if ( dr && dr.GetData(vTemp, 0) )
{
vData.Append(vTemp);
}
//Get minimum and maximum
double dMin, dMax;
vData.GetMinMax(dMin, dMax);
printf("Statistics result of the selected ranges:\n");
printf("Min=%f, Max=%f\n", dMin, dMax);
int nN;
double dMean, dSD, dSum;
int nRet = ocmath_basic_summary_stats(vData.GetSize(), vData, &nN, &dMean, &dSD, NULL, NULL, &dSum);
if(0 == nRet)
{
printf("N=%d, Mean=%f, SD=%f, Sum=%f\n", nN, dMean, dSD, dSum);
}
dr.Reset(); // Reset the data range
vData.SetSize(0); // Reset the vector
}
}
|