gpt4 book ai didi

c# - 将 DataTable 列转换为类型化 IEnumerable[]

转载 作者:行者123 更新时间:2023-12-03 19:43:32 24 4
gpt4 key购买 nike

如何将 DataTable 列转换为在 R.NET 中创建数据框所需的 IEnumerable[]

我有以下代码:

DataTable dt = CreateDateTable();
REngine e = REngine.GetInstance();
IEnumerable[] columns = new IEnumerable[dt.Columns.Count];
string[] columnNames = dt.Columns.Cast<DataColumn>()
.Select(x => x.ColumnName)
.ToArray();

for(int i=0; i<dt.Columns.Count; i++)
//This is the place where I am stuck. How to convert column to base type array instead of object array
columns[i] = dt.Rows.Cast<DataRow>().Select(row => row[i]).ToArray();

DataFrame df = e.CreateDataFrame(columns: columns,
columnNames: columnNames,
stringsAsFactors: false);

我收到以下异常:
Test 'XXX.ReadResultsTest' failed: System.NotSupportedException : Cannot convert type System.Object[] to an R vector
w RDotNet.REngineExtension.ToVector(REngine engine, IEnumerable values)
w System.Array.ConvertAll[TInput,TOutput](TInput[] array, Converter`2 converter)
w RDotNet.REngineExtension.CreateDataFrame(REngine engine, IEnumerable[] columns, String[] columnNames, String[] rowNames, Boolean checkRows, Boolean checkNames, Boolean stringsAsFactors)
DataTable有不同类型的列,我不知道这些类型是什么,所以我不能在这个例子中用 double 做:
for (int i = 0; i < dt.Columns.Count; i++)
columns[i] = dt.Rows.Cast<DataRow>().Select(row => row.Field<double>(i)).ToArray();

更新

到目前为止,我有一个丑陋的解决方案,可以做得更好吗?
for (int i = 0; i < dt.Columns.Count; i++)
{
switch (Type.GetTypeCode(dt.Columns[i].DataType))
{
case TypeCode.String:
columns[i] = dt.Rows.Cast<DataRow>().Select(row => row.Field<string>(i)).ToArray();
break;

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

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

case TypeCode.Int64:
columns[i] = dt.Rows.Cast<DataRow>().Select(row => row.Field<long>(i)).ToArray();
break;

default:
//columns[i] = dt.Rows.Cast<DataRow>().Select(row => row[i]).ToArray();
throw new InvalidOperationException(String.Format("Type {0} is not supported", dt.Columns[i].DataType.Name));
}
}

最佳答案

public DataFrame DataTableToDataFrame(string name, DataTable dt)
{
DataFrame dataFrame = null;

IEnumerable[] columns = new IEnumerable[dt.Columns.Count];
string[] columnNames = dt.Columns.Cast<DataColumn>()
.Select(x => x.ColumnName)
.ToArray();

for (int i = 0; i < dt.Columns.Count; i++)
{
switch (Type.GetTypeCode(dt.Columns[i].DataType))
{
case TypeCode.String:
columns[i] = dt.Rows.Cast<DataRow>().Select(row => row.Field<string>(i)).ToArray();
break;

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

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

case TypeCode.Int64:
case TypeCode.Decimal:
IEnumerable array = dt.Rows.Cast<DataRow>().Select(row => row.Field<object>(i)).ToArray();

//columns[i] = dt.Rows.Cast<DataRow>().Select(row => row.Field<long>(i)).ToArray();
//columns[i] = dt.Rows.Cast<DataRow>().Select(row => row.Field<decimal>(i)).ToArray();

columns[i] = ListToIenumerable(array);
break;

default:
columns[i] = dt.Rows.Cast<DataRow>().Select(row => row[i]).ToArray();
//throw new InvalidOperationException(String.Format("Type {0} is not supported", dt.Columns[i].DataType.Name));
break;
}
}

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

return dataFrame;
}

这是?

关于c# - 将 DataTable 列转换为类型化 IEnumerable[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40877396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com