【发布时间】:2013-09-10 20:57:53
【问题描述】:
我通过 F# 脚本将 excel 数据导入到 R 回归中使用。代码是:
let path = @"C:\Data\DataForRegression.csv"
let fileStream = new FileStream(path,FileMode.Open,FileAccess.Read)
let streamReader = new StreamReader(fileStream)
let contents = streamReader.ReadToEnd()
let cleanContents =
contents.Split([|'\n'|])
|> Seq.map(fun line -> line.Split([|','|]))
|> Seq.skip(1)
|> Seq.map(fun values ->
Double.Parse(values.[0]),
Double.Parse(values.[1]),
DateTime.Parse(values.[2]).ToShortDateString(),
Int32.Parse(values.[3]),
Int32.Parse(values.[4]),
Int32.Parse(values.[5]),
Int32.Parse(values.[6]))
//open R
let environmentPath = System.Environment.GetEnvironmentVariable("PATH")
let binaryPath = @"C:\Program Files\R\R-3.0.1\bin\x64"
System.Environment.SetEnvironmentVariable("PATH",environmentPath+System.IO.Path.PathSeparator.ToString()+binaryPath)
let engine = RDotNet.REngine.CreateInstance("RDotNet")
engine.Initialize()
let pmpm = engine.CreateNumericVector(cleanContents |> Seq.map (fun (a,b,c,d,e,f,g) -> a))
engine.SetSymbol("pmpm",pmpm)
第一行数据如下:
$66.92,0.9458,Jan-13,0,0,0,1
当我运行它时,我得到了这个:
System.FormatException:输入字符串的格式不正确。
在 System.Number.ParseDouble(字符串值,NumberStyles 选项, NumberFormatInfo numfmt) 在 FSI_0002.cleanContents@18.Invoke(String[] values) 在 C:\TFS\Tff.RDotNetExample_Solution\Tff.RDotNetExample\RegressionUsingExcelImport.fsx:line 19 在 Microsoft.FSharp.Collections.IEnumerator.map@109.DoMoveNext(b&) 在 Microsoft.FSharp.Collections.IEnumerator.MapEnumerator1.System-Collections-IEnumerator-MoveNext() at Microsoft.FSharp.Collections.IEnumerator.map@109.DoMoveNext(b& )1.System-Collections-IEnumerator-MoveNext() 在 System.Linq.Enumerable.Count[TSource](IEnumerable
at Microsoft.FSharp.Collections.IEnumerator.MapEnumerator1 source) at RDotNet.Vector1..ctor(REngine 引擎,SymbolicExpressionType 类型, IEnumerable1 vector) at RDotNet.NumericVector..ctor(REngine engine, IEnumerable1 向量)在 RDotNet.REngineExtension.CreateNumericVector(REngine 引擎, IEnumerable`1 向量)在 .$FSI_0002.main@() 在 C:\TFS\Tff.RDotNetExample_Solution\Tff.RDotNetExample\RegressionUsingExcelImport.fsx:line 35 因错误而停止
有人知道我需要做什么来转换数据吗?我的预感是它不喜欢 '$' - 但它加载到 Double.Parse 中没有问题(除非它没有被评估?)。
提前致谢
【问题讨论】:
-
嘿,实际数据的第一行是什么(假设您没有尝试将“PMPM”的文本值解析为双精度值)...