我通过 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(String value, NumberStyles options, NumberFormatInfo numfmt) at 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.MapEnumerator 1.System-Collections-IEnumerator-MoveNext() 在 System.Linq.Enumerable.Count[TSource]( IEnumerable 1..ctor(REngine 引擎,SymbolicExpressionType 类型,IEnumerable1.System-Collections-IEnumerator-MoveNext() at Microsoft.FSharp.Collections.IEnumerator.map@109.DoMoveNext(b& )
at Microsoft.FSharp.Collections.IEnumerator.MapEnumerator1 source) at RDotNet.Vector
1 vector) at RDotNet.NumericVector..ctor(REngine engine, IEnumerable
1 向量)在 RDotNet.REngineExtension.CreateNumericVector(REngine 引擎,IEnumerable`1 向量)在 .$FSI_0002.main@() 在 C:\TFS\Tff.RDotNetExample_Solution\Tff.RDotNetExample\RegressionUsingExcelImport.fsx:line 35 由于停止错误
有谁知道我需要做什么来转换数据?我的预感是它不喜欢 '$' - 但它不会将问题加载到 Double.Parse 中(除非它没有被评估?)。
提前致谢