Passing a seq<string option> from F# to RProvider

随声附和 提交于 2019-12-18 09:37:29

问题


I asked this question last week regarding seq<float option> values passed to RProvider. I had hoped that I'd be able to apply the accepted answer there to other option types in F#. Unfortunately, NaN is only applicable to numeric in R.

How can I convert a None string in F# to NA and pass to R?


回答1:


You can use Option.toObj.

For example:

let l1 = [ Some "x"; None; Some "y"] 

let l2 = l1 |> List.map (Option.toObj)
// val l2 : string list = ["x"; null; "y"]

And you can use Option.toNullable for number values, but it would convert to type Nullable<float>, and None would also be null. For some reason this doesn't work the other way round:

let l3 = l2 |> List.map (Option.ofObj)
// val l3 : string option list = [Some "x"; null; Some "y"]

I don't know if that's intended or a bug.

Edit : Option.ofObj does work properly. F# Interactive displays None as null when it is in a list for some reason.




回答2:


The answer to your previous question is still applicable. You can use null to indicate a missing string value:

let optString = ["x"; null; "y"]

let testData5 =
    namedParams [
        "optString", optString;]
    |> R.data_frame 

Gives me:

val testData5 : SymbolicExpression =
optString
1 x
2 <NA>
3 y

You can convert the option string to just string list:

let optString2 = [Some "x"; None; Some "y"]
optString2 
    |> List.map (fun x -> match x with
                          | Some x -> x
                          | None -> null)


来源:https://stackoverflow.com/questions/39337732/passing-a-seqstring-option-from-f-to-rprovider

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!