Passing decimal values from dataTable to dataFrame fails

时间:2016-06-06 14:19:43

标签: c# dataframe datatable r.net

I have created this code sample to pass an object of type c# DataTable to R.Net dataFrame.

echo base_url('img.jpg');

Everything seems to work fine untill i try to inspect the content of the dataframe. I found that values like public static DataFrame ConvertDataTableToRDataFrame(DataTable tab) { REngine.SetEnvironmentVariables(); REngine engine = REngine.GetInstance(); double?[,] stringData = new double?[tab.Rows.Count, tab.Columns.Count]; DataFrame df = engine.Evaluate("df=NULL").AsDataFrame(); int irow = 0; foreach (DataRow row in tab.Rows) { NumericVector x = engine.Evaluate("x=NULL").AsNumeric(); int icol = 0; foreach (DataColumn col in tab.Columns) { if (row.Field<double?>(col) == null) { x = engine.Evaluate("x=c(x, NA) ").AsNumeric(); } else { x = engine.Evaluate("x=c(x, " + row.Field<double?>(col) + ") ").AsNumeric(); } icol++; } df = engine.Evaluate("df= as.data.frame(rbind(df,x)) ").AsDataFrame(); irow++; } return (df); } turn in the dataframe to 1.2355. For some unknown reason it doesn't recognize 12355 as decimal separator.

1 个答案:

答案 0 :(得分:1)

- 添加@Panagiotis Kanavos命题作为答案,因为它解决了问题 -

小数没有任何特定的小数分隔符,它们是二进制值。仅当分隔符转换为字符串时才使用分隔符。根据定义,Role.first=> #<Role id: 1, name: "admin", admin_user_id: 1, resource_id: nil, resource_type: nil, created_at: "2016-06-16 15:03:33", updated_at: "2016-06-17 09:29:32"> Role.first.admin_users => [] 的输出将是row.Field<double?>(col),但是将其连接到字符串会使用当前用户的文化将其转换为字符串。

改为使用Nullable<double>String.Format(CultureInfo.InvariantCulture,"x=c(x, {0})" ,row.Field<double?>(col)) -