如何使用RTypeProvider创建多种类型的数据框 [英] How to create a dataframe of multiple types with the RTypeProvider

查看:80
本文介绍了如何使用RTypeProvider创建多种类型的数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎RTypeProvider只能处理相同类型的namedParams.是这样吗

例如

open RDotNet
open RProvider 

type foo = {
    Which: string
    Qty: float option
    }    

let someFoos = [{Which = "that"; Qty = Some 4.0}; {Which = "other"; Qty = Some 2.0}]

let thingForR = 
    namedParams [
        "which", someFoos |> List.map (fun x -> x.Which); 
        "qty", someFoos |> List.map (fun x -> x.Qty);
        ]
    |> R.data_frame

不起作用,因为我在x.Qty

时出现错误

This expression was expected to have type 
  string
but here has type
  float option

如果我颠倒thingForR let中的顺序,那么我得到相反的错误:

let thingForR = 
    namedParams [
        "qty", someFoos |> List.map (fun x -> x.Qty); 
        "which", someFoos |> List.map (fun x -> x.Which);
        ]
    |> R.data_frame

在这里,x.Which上的错误是

This expression was expected to have type
  float option
but here has type
  string

namedParams中的词典可以没有不同的类型吗?如果是这样,如何在F#中创建具有不同类型的数据框并将其传递给R?

解决方案

您需要将字典中的值装箱.这样,它们都只是对象.所以:

let thingForR = 
    namedParams [
        "which", box (someFoos |>  List.map (fun x ->  x.Which) ); 
        "qty", box  (someFoos |> List.map (fun x ->   x.Qty) |> List.map (Option.toNullable >> float));
        ]
    |> R.data_frame

给我:

val somethingForR:
SymbolicExpression =哪个qty
1等于4
另外2个

请在浮动选项上参考您之前的问题将Option list转换为float list.如有必要,还字符串选项.

您可以遍历Deedle(如果不包含选项值):

let someFoos' = [{Which = "that"; Qty =  4.0}; {Which = "other"; Qty =  2.0}]
let df' = someFoos' |> Frame.ofRecords
df' |> R.data_frame 

It seems like the RTypeProvider can only handle namedParams of the same type. Is this the case?

For example,

open RDotNet
open RProvider 

type foo = {
    Which: string
    Qty: float option
    }    

let someFoos = [{Which = "that"; Qty = Some 4.0}; {Which = "other"; Qty = Some 2.0}]

let thingForR = 
    namedParams [
        "which", someFoos |> List.map (fun x -> x.Which); 
        "qty", someFoos |> List.map (fun x -> x.Qty);
        ]
    |> R.data_frame

doesn't work as I get an error on the x.Qty saying

This expression was expected to have type 
  string
but here has type
  float option

If I reverse the order in the thingForR let, then I get the opposite error:

let thingForR = 
    namedParams [
        "qty", someFoos |> List.map (fun x -> x.Qty); 
        "which", someFoos |> List.map (fun x -> x.Which);
        ]
    |> R.data_frame

Here, the error on x.Which is

This expression was expected to have type
  float option
but here has type
  string

Can the dictionary in the namedParams not have different types? If so, how can you create a data frame with different types in F# and pass them to R?

解决方案

You need to box the values inside the dictionary. That way they are all just object. So:

let thingForR = 
    namedParams [
        "which", box (someFoos |>  List.map (fun x ->  x.Which) ); 
        "qty", box  (someFoos |> List.map (fun x ->   x.Qty) |> List.map (Option.toNullable >> float));
        ]
    |> R.data_frame

gives me:

val thingForR :
SymbolicExpression = which qty
1 that 4
2 other 2

Please refer to your previous question on float option to convert the Option list to float list. Also string option if necessary.

You can go through Deedle (if not for the option values):

let someFoos' = [{Which = "that"; Qty =  4.0}; {Which = "other"; Qty =  2.0}]
let df' = someFoos' |> Frame.ofRecords
df' |> R.data_frame 

这篇关于如何使用RTypeProvider创建多种类型的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆