This project is read-only.

How can I Convert DataTable(C#) to Dataframe(R)?

Jun 21, 2012 at 4:34 AM
Edited Jun 21, 2012 at 4:35 AM

I use C# code to read data from SQL Server and choose DataTabe to stored the data.

But I don't know how to convert the DataTable to Dataframe in R.NET.

I notice in in R.NET bridges between R and .NET Framework. But it not include data frame.

In R.NET,it's not provide Constructor function to create a dataframe instance.

I must convert table to a CSV,and use read.table() like this:


string data = RHelper.DataTableToCSV(dt);
DataFrame tb = engine.EagerEvaluate("tb<-read.table(text='" + data + "',sep=',',header=TRUE,,na.strings = 'd')").AsDataFrame();


It's can't control the every column datatype in R.

Some one has good ideal?Thanks for looking my post.

Jun 21, 2012 at 5:05 PM


Assuming you have say a DataTable of numbers. Take your DataTable object and iterate through the rows of each column to build a collection of List<double> for each column - I assume you can do this so I wont put the code in.

So for example you may have two columns in your DataTable and this would give you something like:

  List<double> col1
  List<double> col2

Now create a NumericVector for each one eg:

   NumericVector nvCol1 = engine.CreateNumericVector(col1);
   NumericVector nvCol2 = engine.CreateNumericVector(col2); 

   engine.SetSymbol("col1", nvCol1);
   engine.SetSymbol("col2", nvCol2);      


Now create a string expression to form a DataFrame:

   string dataFrameExpression = "data=data.frame(cbind(col1,col2))";

Now do something like:

   var result = engine.Evaluate(dataFrameExpression).AsDataFrame();