VB Apply Transform Example

← All NMath Stats Code Examples

 

Imports System

Imports CenterSpace.NMath.Core
Imports CenterSpace.NMath.Stats

Namespace CenterSpace.NMath.Stats.Examples.VisualBasic

  ' A .NET example in Visual Basic showing how to use the Apply and Transform methods on 
  ' a DataFrame and its columns.
  Module ApplyTransformExample

    Sub Main()

      Dim Data As DataFrame = 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"))
      Data = InitializeData(Data)

      Console.WriteLine()
      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.
      Dim Concatenate As New Func(Of String, String, String)(AddressOf CombineNames)
      Dim FullNames As DFStringColumn = (CType(Data("first"), DFStringColumn).Apply("full names", Concatenate, CType(Data("last"), DFStringColumn)))
      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
      CType(Data("test2"), DFNumericColumn).Transform(New Func(Of Double, Double)(AddressOf Scale))

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

      ' Cap all grades at 95
      CType(Data("test1"), DFNumericColumn).Transform(New Func(Of Double, Double)(AddressOf Cap))
      CType(Data("test2"), DFNumericColumn).Transform(New Func(Of Double, Double)(AddressOf 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
      Dim WeightedFunction As New Func(Of Double, Double, Double)(AddressOf Weighted)
      Dim Test1 As DFNumericColumn = CType(Data("test1"), DFNumericColumn)
      Dim Test2 As DFNumericColumn = CType(Data("test2"), DFNumericColumn)
      Dim Final As DFNumericColumn = CType(Test1.Apply("final", WeightedFunction, Test2), DFNumericColumn)
      Data.AddColumn(Final)

      Console.WriteLine()
      Console.WriteLine("Final weighted average...")
      Console.WriteLine()
      Console.WriteLine(Data)

      ' Round all numeric values to the nearest whole number
      CType(Data("test1"), DFNumericColumn).Transform(New Func(Of Double, Integer, Double)(AddressOf Math.Round), 1)
      CType(Data("test1"), DFNumericColumn).NumericFormat = "F1"

      CType(Data("test2"), DFNumericColumn).Transform(New Func(Of Double, Integer, Double)(AddressOf Math.Round), 1)
      CType(Data("test2"), DFNumericColumn).NumericFormat = "F1"

      CType(Data("final"), DFNumericColumn).Transform(New Func(Of Double, Integer, Double)(AddressOf Math.Round), 1)
      CType(Data("final"), DFNumericColumn).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()
    End Sub

    Function InitializeData(ByRef Data As DataFrame) As DataFrame
      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)
      Return Data
    End Function

    Function CombineNames(ByVal First As String, ByVal Last As String) As String
      Return First + " " + Last
    End Function

    Function Scale(ByVal Score As Double) As Double
      Return Score + 10.0
    End Function

    Function Cap(ByVal Score As Double) As Double
      If Score > 95.0 Then
        Return 95.0
      Else
        Return Score
      End If
    End Function

    Function Weighted(ByVal Test1 As Double, ByVal Test2 As Double) As Double
      Return (0.4 * Test1) + (0.6 * Test2)
    End Function

  End Module

End Namespace


← All NMath Stats Code Examples
Top