Statsar Statistics Library

v1.0.1 for .NET

Product Guide



More Information...

Valid XHTML


Group Data Example (C#)

[C#]
/* COPYRIGHT(C), SIMPLEXAR SOFTWARE LIMITED, 2006-2008.
 * All rights reserved.
 *
 * Use of this copyright notice is precautionary only, and does not imply
 * publication or disclosure. The content of this work contains confidential
 * and proprietary information of Simplexar Software Limited. Any duplication,
 * modification, distribution, or disclosure in any form, in whole, or in part,
 * is strictly prohibited without express prior written permission.
 */

// Get namespaces.
using System;
using Simplexar.Statsar;
using Simplexar.Statsar.Filters;

// Start namespace.
namespace Simplexar.Examples.Statsar
{
    /// <summary>
    /// This example illustrates how to group data. When grouping data,
    /// rows are combined together from the original data to produce a set
    /// of summary rows.
    /// </summary>
    public static class Example
    {
        /// <summary>
        /// The application entry point.
        /// </summary>
        public static void Main()
        {
            try
            {
                RunExample();
            }
            catch(Exception exception)
            {
                Console.WriteLine(exception);
            }
            Console.WriteLine("Press ENTER to terminate.");
            Console.ReadLine();
        }

        private static void RunExample()
        {
            // Create sheet.
            StatsCalculator calculator = new StatsCalculator();
            DataSheet sheet = calculator.Sheet;
            ColumnList columns = sheet.Columns;
            columns.Add("Name", typeof(string));
            columns.Add("Height", typeof(double));
            columns.Add("Weight", typeof(double));
            columns.Add("Jog", typeof(bool));
            columns.Add("Tennis", typeof(bool));

            // Populate data.
            RowList rows = sheet.Rows;
            rows.Add("Robert", 68, 135, true, true);
            rows.Add("George", 67, 180, false, false);
            rows.Add("Agatha", 63, 110, false, false);
            rows.Add("Sandy", 60, 125, true, true);
            rows.Add("Bill", 65, 160, true, false);
            Console.WriteLine(sheet);

            // Given the above data, suppose we are interested in considering
            // two groups of people, those who jog and those who do not jog.
            // We can summarize the data above by using a sheet filter. For
            // each of these two groups, suppose we wish to calculate the
            // number of people in the group, and the average weight.
            // The select expressions we are interested in are: "Jog",
            // "COUNT(Jog)" and "Mean = MEAN(Weight)". The group expression we
            // are interested in is "Jog".

            // Configure sheet filter.
            SheetFilter filter = new SheetFilter();
            filter.Select("Jog", "COUNT(JOG)", "Mean = MEAN(Weight)");
            filter.GroupBy("Jog");

            // Filter data to produce summary.
            DataSheet summarySheet = sheet.Filter(filter);
            Console.WriteLine(summarySheet);
        }
    }
}