2

I need to convert a data table which I have defined as follows to a data frame in R.

DataTable dtb = new DataTable();
            dtb.Columns.Add("Column1", Type.GetType("System.String"));
            dtb.Columns.Add("Column2", Type.GetType("System.String"));
            DataRow dtr1 = dtb.NewRow();
            dtr1[0] = "abc";
            dtr1[1] = "cdf";
            dtb.Rows.Add(dtr1);
            DataRow dtr2 = dtb.NewRow();
            dtr2[0] = "asdasd";
            dtr2[1] = "cdasdasf";
            dtb.Rows.Add(dtr2);

Is there any way to convert above DataTable "dtb" to a data.frame in R. I have defined this DataTable in C# but I need to do computation in R, that is why I need to pass it to R.

4
  • From what I can tell, it seems the best approach is to either convert the data table to a numeric matrix, or to convert it to a string. I haven't passed data from a C# application to R myself (yet), but it does seem that using C# strings as commands to the R engine is generally the best way to get commands and objects defined in the R environment. (see stackoverflow.com/questions/32022831/…; stackoverflow.com/questions/32029438/…). Commented Oct 20, 2015 at 11:20
  • Also, if at all possible, I'd recommend placing any data you may want to use in a database (MySQL, or some other) and then using R's RODBC package to pull the data into R. That seems like an approach that plays to the strengths of both programs. Commented Oct 20, 2015 at 11:21
  • @Benjamin, I converted DataTable to a string, but then how can I use it? Commented Oct 20, 2015 at 12:16
  • How about casting the DataTable to array and using the R.NET converter functions? github.com/jmp75/rdotnet/blob/master/R.NET/Utilities/… Commented Oct 20, 2015 at 20:17

2 Answers 2

5

This worked in R.NET 1.6.5 and outputs:

output from console application

Did not find a nice way of converting DataTable to string[,] as CreateCharacterMatrix expects but CreateDataFrame is an alternative possibility but that expects a IEnumerable[] and is column oriented (as data.frame in R is).

Going through the Tests in the source code might help further: https://github.com/jmp75/rdotnet/tree/master/RDotNet.Tests

There is also this: https://github.com/jmp75/rdotnet-onboarding

Which is in some ways more helpful because the official documentation is sparse: https://jmp75.github.io/rdotnet/getting_started/

using System;
using System.Data;
using RDotNet;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dtb = new DataTable();
            dtb.Columns.Add("Column1", Type.GetType("System.String"));
            dtb.Columns.Add("Column2", Type.GetType("System.String"));
            DataRow dtr1 = dtb.NewRow();
            dtr1[0] = "abc";
            dtr1[1] = "cdf";
            dtb.Rows.Add(dtr1);
            DataRow dtr2 = dtb.NewRow();
            dtr2[0] = "asdasd";
            dtr2[1] = "cdasdasf";
            dtb.Rows.Add(dtr2);

            using (var engine = REngine.GetInstance())
            {
                string[,] stringData = new string[dtb.Rows.Count, dtb.Columns.Count];
                for (int row = 0; row < dtb.Rows.Count; row++)
                {
                    for (int col = 0; col < dtb.Columns.Count; col++)
                    {
                        stringData[row, col] = dtb.Rows[row].ItemArray[col].ToString();
                    }
                }
                CharacterMatrix matrix = engine.CreateCharacterMatrix(stringData);
                engine.SetSymbol("myRDataFrame", matrix);
                engine.Evaluate("myRDataFrame <- as.data.frame(myRDataFrame, stringsAsFactors = FALSE)");
                engine.Evaluate("str(myRDataFrame)");

            }
            Console.ReadKey();
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I think u can get your solution here.. Passing DataTable over COM into R This one has better explanation

2 Comments

Say, I get a string by converting DataTable to a string. Now how can I pass it to R.net. Because string is not acceptable. string csvfile= DataTableToCSV(dtb);
If you're able to make it a CSV, you should be able to read it into R with read.csv. If you've saved it to a file, you can use just plain read.csv. If you've saved it to a character string to pass as an object to R, you can use read.csv(textConnection(...))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.