C# Apply Transform Example

← All NMath Code Examples

 

using System;

using CenterSpace.NMath.Core;

namespace CenterSpace.NMath.Examples.CSharp
{
  /// <summary>
  /// A .NET example in C# showing how to use the Apply and Transform methods on 
  /// a DataFrame and its columns.
  /// </summary>
  class ApplyTransformExample
  {
    private static DataFrame data;

    static void Main( string[] args )
    {
      data = new DataFrame();
      data.AddColumn( new DFStringColumn( "first" ) );
      data.AddColumn( new DFStringColumn( "last" ) );
      data.AddColumn( new DFDateTimeColumn( "birthdate" ) );
      data.AddColumn( new DFNumericColumn( "test1" ) );
      data.AddColumn( new DFNumericColumn( "test2" ) );
      InitializeData();

      Console.WriteLine();
      Console.WriteLine( "Original data..." );
      Console.WriteLine();
      Console.WriteLine( data );

      // Combine first name and last name columns into a new 
      // column and then add it to the data frame.
      var concatenate = new Func<String, String, String>( CombineNames );
      var fullNames = ( (DFStringColumn) data["first"] ).Apply( "full names", concatenate,
        (DFStringColumn) data["last"] );
      data.InsertColumn( 0, fullNames );

      // Remove first name, last name columns
      data.RemoveColumn( "first" );
      data.RemoveColumn( "last" );

      Console.WriteLine();
      Console.WriteLine( "Concatenate first and last names..." );
      Console.WriteLine();
      Console.WriteLine( data );

      /// Scale all test2 scores up by 10 points
      ( (DFNumericColumn) data["test2"] ).Transform( new Func<double, double>( Scale ) );

      Console.WriteLine();
      Console.WriteLine( "Scale test2 up by 10%..." );
      Console.WriteLine();
      Console.WriteLine( data );

      // Cap all grades at 95
      ( (DFNumericColumn) data["test1"] ).Transform( new Func<double, double>( Cap ) );
      ( (DFNumericColumn) data["test2"] ).Transform( new Func<double, double>( Cap ) );

      Console.WriteLine();
      Console.WriteLine( "Cap grades at 95%..." );
      Console.WriteLine();
      Console.WriteLine( data );

      // Add a new column which is the weighted average of the two tests
      var weighted = new Func<double, double, double>( Weighted );
      var final = ( (DFNumericColumn) data["test1"] ).Apply( "final", weighted, (DFNumericColumn) data["test2"] );
      data.AddColumn( final );

      Console.WriteLine();
      Console.WriteLine( "Final weighted average..." );
      Console.WriteLine();
      Console.WriteLine( data );

      // Round all numeric values to the nearest whole number
      ( (DFNumericColumn) data["test1"] ).Transform( new Func<double, int, double>( System.Math.Round ), 1 );
      ( (DFNumericColumn) data["test1"] ).NumericFormat = "F1";

      ( (DFNumericColumn) data["test2"] ).Transform( new Func<double, int, double>( System.Math.Round ), 1 );
      ( (DFNumericColumn) data["test2"] ).NumericFormat = "F1";

      ( (DFNumericColumn) data["final"] ).Transform( new Func<double, int, double>( System.Math.Round ), 1 );
      ( (DFNumericColumn) data["final"] ).NumericFormat = "F1";

      Console.WriteLine();
      Console.WriteLine( "Round to one decimal place..." );
      Console.WriteLine();
      Console.WriteLine( data );

      Console.WriteLine();
      Console.WriteLine( "Press Enter Key" );
      Console.Read();
    }

    private static void InitializeData()
    {
      data.AddRow( 1, "Jack", "Cooper", "1/29/1968", 55.4, 63.3 );
      data.AddRow( 2, "Henri", "Robert", "12/4/1978", 85.6, 90.2 );
      data.AddRow( 3, "Sasha", "Klein", "5/7/1947", 65.4, 64.3 );
      data.AddRow( 4, "Gail", "Blake", "8/4/1985", 75.1, 48.2 );
      data.AddRow( 5, "Paul", "Norton", "9/21/1981", 55.4, 62.3 );
      data.AddRow( 6, "Harry", "Liu", "10/12/1972", 85.6, 87.3 );
      data.AddRow( 7, "Roberto", "Ortiz", "11/3/1978", 59.2, 93.3 );
      data.AddRow( 8, "Marie", "Flint", "4/5/1972", 66.7, 38.2 );
    }

    private static string CombineNames( string first, string last )
    {
      return first + " " + last;
    }

    private static double Scale( double score )
    {
      return score + 10.0;
    }

    private static double Cap( double score )
    {
      return score > 95.0 ? 95.0 : score;
    }

    private static double Weighted( double test1, double test2 )
    {
      return ( 0.4 * test1 ) + ( 0.6 * test2 );
    }

  }  // class

}  // namespace


← All NMath Code Examples
Top