繁体   English   中英

F#、Deddle 和 R.Net:seq<'a> [] 与 IEnumerable 数组

[英]F#, Deddle and R.Net: seq<'a> [] vs. IEnumerable array

我正在尝试使用 R.Net 将 Deedle Frame 转换为 R DataFrame。

我尝试了 3 种不同的方法:

open RDotNet

REngine.SetEnvironmentVariables(@"C:\Program Files\R\R-3.6.3\bin\x64",@"C:\Program Files\R\R-3.6.3")

let engine = REngine.GetInstance()

//Deedle with columns x and y and with all the values as floats
let d = Frame.ReadCsv(@"C:\Users\flavi\Downloads\test.txt")

let a1 =
    ([],d.ColumnKeys)
    ||> Seq.fold (fun acc elem -> (d |> Frame.getCol elem |> Series.values |> Seq.map float)::acc) |> List.toArray

let a2 =
    [|
        d |> Frame.getCol "x" |> Series.values |> Seq.map float
        d |> Frame.getCol "y" |> Series.values |> Seq.map float
    |]

let (a3 : IEnumerable array) =
    [|
        d |> Frame.getCol "x" |> Series.values |> Seq.map float
        d |> Frame.getCol "y" |> Series.values |> Seq.map float
    |]

//only works with a3
let rFrame = engine.CreateDataFrame(a3,d.ColumnKeys |> Seq.map string |> Seq.toArray)

a1(我想使用的那个)和 a2 具有相同的签名:seq < float > []。 a3 与 a2 相同,唯一的区别是 IEnumerable 数组的“强制”签名。 只有 a3 有效,但 a3 的创建方式不好,因为我必须手动插入所有列。

我的问题是:1) 为什么只有 a3 有效?; 2) 我怎样才能像 a1 一样重新创建 a3,即不必事先知道所有存在的列并且具有 IEnumerable 数组作为签名?

CreateDataFrame() 需要一个IEnumerable数组,但 a1 是一个seq<float>数组,这是 F# 的说法IEnumerable<float> 你只需要另一个演员在那里。 这为我编译(虽然我还没有真正运行它):

let a1 =
    d.ColumnKeys
    |> Seq.fold 
        (fun acc key -> 
            let values =
                d 
                |> Frame.getCol key
                |> Series.values 
                |> Seq.map unbox<float>
            values::acc)
        []
    |> Seq.map (fun floats -> floats :> IEnumerable)
    |> Seq.toArray

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM