0

I'm trying to convert some R code script into C# code via RDotNet Library. I was able to convert a big part but there's an issue while creating data.table in C#. Above is my code example:

Dictionary<string, List<string>> myDic = new Dictionary<string, List<string>>();
// populate the dictionary
var colNames = new List<string>() { "col1", "col2"};
IEnumerable[] columns = new IEnumerable[2];
columns[0] = myDic.Keys; 
columns[1] = myDic.Values;

var mydata = engine.CreateDataFrame(columns.ToArray(), colNames.ToArray(), stringsAsFactors: false);

when i use the variable mydata, I got an exception

Cannot convert type System.Collections.Generic.List`1[System.String][] to an R vector

mhd
  • 446
  • 5
  • 18
  • I do not speak C#, but the function is called `CreateDataFrame` so I assume it creates a data.frame and not a data.table? – Roland Apr 05 '16 at 13:15

1 Answers1

0

Are you sure you want to create a dataframe column that contains a list of strings in each cell? It's not how you create a dataframe in R. The code below works when you replace Dictionary<string,List<string>> with Dictionary<string,string>.

        Dictionary<string, string> myDic = new Dictionary<string, string>() {
            { "a","aaaa"},
            { "b","bbbb"},
            { "c","cccc"}
        };
        // populate the dictionary
        var colNames = new List<string>() { "col1", "col2" };
        IEnumerable[] columns = new IEnumerable[2];
        columns[0] = myDic.Keys.ToArray();
        columns[1] = myDic.Values.ToArray();

        REngine engine = REngine.GetInstance();
        var mydata = engine.CreateDataFrame(columns, colNames.ToArray(), stringsAsFactors: false);
Robert
  • 924
  • 1
  • 9
  • 20