This project is read-only.

How to Query STRING VALUES from Dataframe

Feb 13, 2014 at 4:26 AM
I'm a beginner..I wrote something like this:
        engine.Evaluate("library(ggplot2)");
        engine.Evaluate("data(mpg)");
        DataFrame df = engine.GetSymbol("mpg").AsDataFrame();
        for (int rowIndex = 0; rowIndex < df.RowCount; rowIndex++)
        {
            for (int colIndex = 0; colIndex < df.ColumnCount; colIndex++)
            {
                Console.Write(df[rowIndex, colIndex].ToString() + " ");
            }
            Console.Write(Environment.NewLine);
        }
I hope it returns like this:
    manufacturer  model displ year cyl trans drv cty hwy fl class
1           audi                     a4   1.8 1999   4   auto(l5)   f  18  29  p    compact
2           audi                     a4   1.8 1999   4 manual(m5)   f  21  29  p    compact
3           audi                     a4   2.0 2008   4 manual(m6)   f  20  31  p    compact
4           audi                     a4   2.0 2008   4   auto(av)   f  21  30  p    compact
...
but it returns like this
1 2 1.8 1999 4 4 2 18 29 4 2
1 2 1.8 1999 4 9 2 21 29 4 2
1 2 2 2008 4 10 2 20 31 4 2
1 2 2 2008 4 1 2 21 30 4 2
....
why&How to do?
Feb 13, 2014 at 10:36 AM
This is because the columns of the data frames are vectors that are factors. Even in pure R coercion of factors can be a stumbling block.

I suggest you access the data frame by columns, like the following which gives the expected answer.
         engine.Evaluate("data(mpg, package='ggplot2')");
         var mpg = engine.Evaluate("mpg").AsDataFrame();
         var manufacturer = mpg[0].AsFactor().GetFactors();
         Assert.AreEqual("audi", manufacturer[0]);  // Vorsprung durch technik
I'll open a feature request to see whether R.NET can match the behavior of R.
Feb 14, 2014 at 3:08 AM
Edited Feb 14, 2014 at 3:15 AM
Works correctly
Thanks to @jperraud..