简体   繁体   English

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

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

I'm trying to convert a Deedle Frame into an R DataFrame using R.Net.我正在尝试使用 R.Net 将 Deedle Frame 转换为 R DataFrame。

I tried 3 different approaches:我尝试了 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 (the one I would like to use) and a2 have the same signature: seq < float > []. a1(我想使用的那个)和 a2 具有相同的签名:seq < float > []。 a3 is the same as a2, the only difference being the “forced” signature of IEnumerable array. a3 与 a2 相同,唯一的区别是 IEnumerable 数组的“强制”签名。 Only a3 works, but the way a3 was created is not good, since I have to insert all the columns by hand.只有 a3 有效,但 a3 的创建方式不好,因为我必须手动插入所有列。

My questions are: 1) why only a3 works?;我的问题是:1) 为什么只有 a3 有效?; 2) how can I recreate a3 like a1, ie, not having to know beforehand all the columns that exist and having IEnumerable array as signature? 2) 我怎样才能像 a1 一样重新创建 a3,即不必事先知道所有存在的列并且具有 IEnumerable 数组作为签名?

CreateDataFrame() wants an array of IEnumerable , but a1 is an array of seq<float> , which is F#'s way of saying IEnumerable<float> ; CreateDataFrame() 需要一个IEnumerable数组,但 a1 是一个seq<float>数组,这是 F# 的说法IEnumerable<float> you just need another cast in there.你只需要另一个演员在那里。 This compiles for me (though I haven't actually run it):这为我编译(虽然我还没有真正运行它):

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