Use R in C#: Execute Functions

As shown in a previous article it is easy to integrate R in a C# application by using R.Net. Within this article I want to explain how to call R functions.

 
Use Case

The R function calls should be shown by calculating the correlation coefficient between two data sets. In this example I want to analyze the correlation between the size and population of countries. The data in this example is based on real data with size in thousand square kilometers and population in thousand inhabitants.

 
Calculate correlation coefficient

The basic data type in R is a vector. Therefore we will start to create two vectors with the needed data. The following source code contains the two data lists in C# and the code to create the according R vectors. The function call “SetSymbol” introduces the vectors to the R engine and give them an alias name. Later on we will access the data by using these alias names.

List<int> size = new List<int>() { 29, 33, 51, 110, 357, 45, 338, 543, 132, 70, 103, 301, 146, 10, 56, 243, 238 };
List<int> population = new List<int>() { 3162, 11142, 3834, 7305, 81890, 1339, 5414, 65697, 11280, 4589, 320, 60918, 480, 1806, 4267, 63228, 21327 };

IntegerVector sizeVector = engine.CreateIntegerVector(size);
engine.SetSymbol("size", sizeVector);

IntegerVector populationVector = engine.CreateIntegerVector(population);
engine.SetSymbol("population", populationVector);

 

At next we can call the R function to calculate the correlation coefficient. This is done by calling the according function “cor” and hand over the two vectors. The result itself is a vector too. We could get the result by accessing the first element of this result vector.

NumericVector resultVector = engine.Evaluate("cor(size, population)").AsNumeric();
result = resultVector[0].ToString();

 
Application

The following source code shows the complete example as console application.

using System;
using RDotNet;
using System.Collections.Generic;

namespace R_FunctionCall
{
    class Program
    {
        static void Main(string[] args)
        {
            string result;
            REngine engine;

            //init the R engine            
            REngine.SetEnvironmentVariables();
            engine = REngine.GetInstance();
            engine.Initialize();

            //prepare data
            List<int> size = new List<int>() { 29, 33, 51, 110, 357, 45, 338, 543, 132, 70, 103, 301, 146, 10, 56, 243, 238 };
            List<int> population = new List<int>() { 3162, 11142, 3834, 7305, 81890, 1339, 5414, 65697, 11280, 4589, 320, 60918, 480, 1806, 4267, 63228, 21327 };

            //calculate
            IntegerVector sizeVector = engine.CreateIntegerVector(size);
            engine.SetSymbol("size", sizeVector);

            IntegerVector populationVector = engine.CreateIntegerVector(population);
            engine.SetSymbol("population", populationVector);

            NumericVector resultVector = engine.Evaluate("cor(size, population)").AsNumeric();
            result = resultVector[0].ToString();

            //clean up
            engine.Dispose();

            //output
            Console.WriteLine("");
            Console.WriteLine("Result: '{0}'", result);
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
    }
}
Advertisements
Dieser Beitrag wurde unter .NET, C#, R veröffentlicht. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s