// new imp
        public DataFrame TimeSeriesToDataFrame(IEnumerable <TimeSeriesIndex> input, REngine engine)
        {
            var dateSelect   = input.Select(x => x.Date).ToArray();
            var closeSelect  = input.Select(x => x.Close).ToArray();
            var openSelect   = input.Select(x => x.Open).ToArray();
            var highSelect   = input.Select(x => x.High).ToArray();
            var lowSelect    = input.Select(x => x.Low).ToArray();
            var VolumeSelect = input.Select(x => x.Volume).ToArray();

            string[] stringDate   = dateSelect.Select(x => x.ToShortDateString()).ToArray();
            double[] doubleClose  = Array.ConvertAll(closeSelect, x => (double)x);
            double[] doubleOpen   = Array.ConvertAll(openSelect, x => (double)x);
            double[] doubleHigh   = Array.ConvertAll(highSelect, x => (double)x);
            double[] doubleLow    = Array.ConvertAll(lowSelect, x => (double)x);
            double[] doubleVolume = Array.ConvertAll(VolumeSelect, x => (double)x);

            IEnumerable[] RowDatasets = new IEnumerable[6];
            RowDatasets[0] = stringDate;
            RowDatasets[1] = doubleClose;
            RowDatasets[2] = doubleOpen;
            RowDatasets[3] = doubleHigh;
            RowDatasets[4] = doubleLow;
            RowDatasets[5] = doubleVolume;

            var RowcolumnNames = new[] { "Date", "Close", "Open", "High", "Low", "Volume" };

            return(engine.CreateDataFrame(RowDatasets, columnNames: RowcolumnNames));
        }
Beispiel #2
0
        private static void TestIssue53(REngine engine, int nrow)
        {
            DataFrame df;

            IEnumerable[] columns = new IEnumerable[9];
            var           a       = new string[nrow];
            var           b       = new int[nrow];
            var           c       = new double[nrow];
            var           r       = new System.Random(333);
            int           modulo  = 333;

            for (int i = 0; i < nrow; i++)
            {
                var x = r.Next();
                a[i] = (x % modulo).ToString();
                b[i] = x % modulo;
                c[i] = i + 0.1;
            }
            columns[0] = a;
            columns[1] = b;
            columns[2] = c;
            columns[3] = b;
            columns[4] = c;
            columns[5] = b;
            columns[6] = c;
            columns[7] = b;
            columns[8] = c;
            string[] columnNames = null;

            df = engine.CreateDataFrame(columns);
            var a_out = df[0].AsCharacter().ToArray();

            var ints = new int[4][];
            var nums = new double[4][];

            for (int i = 0; i < 4; i++)
            {
                ints[i] = df[1 + i * 2].AsInteger().ToArray();
                nums[i] = df[2 + i * 2].AsNumeric().ToArray();
            }
            for (int i = 0; i < nrow; i++)
            {
                if (a[i] != a_out[i])
                {
                    Console.WriteLine("Mismatch A at index {0}, {1} != {2}", i, a_out[i], a[i]); break;
                }
                for (int j = 0; j < 4; j++)
                {
                    if (b[i] != ints[j][i])
                    {
                        Console.WriteLine("Mismatch B at index {0} col rep {1}, {2} != {3}", i, j, ints[j][i], b[i]); break;
                    }
                    if (c[i] != nums[j][i])
                    {
                        Console.WriteLine("Mismatch C at index {0} col rep {1}, {2} != {3}", i, j, nums[j][i], c[i]); break;
                    }
                }
            }
        }
Beispiel #3
0
        private static void TestDataFrameInMemoryCreation(REngine engine)
        {
            IEnumerable[] columns;
            string[]      columnNames;
            DataFrame     df;

            createTestDataFrame(engine, out columns, out columnNames, out df);
            df         = engine.CreateDataFrame(columns, columnNames: null);
            columns[1] = new int[] { 1, 2, 3, 4, 5, 6, 7 };
            try
            {
                df = engine.CreateDataFrame(columns, columnNames: null);
            }
            catch (Exception ex)
            {
                counter++;
                Console.WriteLine(string.Format("Caught an exception ({0})", counter));
                Console.WriteLine(ex.ToString());
            }
            Console.WriteLine("Recovered from evaluation exception?");
        }
Beispiel #4
0
        /// <summary>
        /// DataTable -> DataFrame 변환
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dtdata"></param>
        /// <returns></returns>
        private DataFrame convertTOFrame(REngine rEngine, string name, DataTable dtdata)
        {
            try
            {
                DataFrame     dataFrame   = null;
                IEnumerable[] columns     = new IEnumerable[dtdata.Columns.Count];
                string[]      columnNames = dtdata.Columns.Cast <DataColumn>()
                                            .Select(x => x.ColumnName)
                                            .ToArray();
                for (int i = 0; i < dtdata.Columns.Count; i++)
                {
                    switch (Type.GetTypeCode(dtdata.Columns[i].DataType))
                    {
                    case TypeCode.String:
                        columns[i] = dtdata.Rows.Cast <DataRow>().Select(row => row.Field <string>(i)).ToArray();
                        break;

                    case TypeCode.Double:
                        columns[i] = dtdata.Rows.Cast <DataRow>().Select(row => row.Field <double>(i)).ToArray();
                        break;

                    case TypeCode.Int32:
                        columns[i] = dtdata.Rows.Cast <DataRow>().Select(row => row.Field <int>(i)).ToArray();
                        break;

                    case TypeCode.Decimal:
                        columns[i] = dtdata.Rows.Cast <DataRow>().Select(row => Convert.ToDouble(row.Field <decimal>(i))).ToArray();
                        break;

                    default:
                        columns[i] = dtdata.Rows.Cast <DataRow>().Select(row => row[i]).ToArray();
                        break;
                    }
                }

                dataFrame = rEngine.CreateDataFrame(columns: columns, columnNames: columnNames, stringsAsFactors: false);
                rEngine.SetSymbol(name, dataFrame);

                return(dataFrame);
            }
            catch (Exception ex)
            {
                return(null);

                throw ex;
            }
        }
Beispiel #5
0
 private static void createTestDataFrame(REngine engine, out IEnumerable[] columns, out string[] columnNames, out DataFrame df)
 {
     columns     = createTestDfColumns();
     columnNames = new[] { "Category", "No.", "Measure" };
     df          = engine.CreateDataFrame(columns, columnNames: columnNames);
 }