RProvider icon indicating copy to clipboard operation
RProvider copied to clipboard

Allow NAs to be represented as Option types at the F# boundary

Open kevinykuo opened this issue 11 years ago • 3 comments

I'm currently passing .NET NaN's to R via RDotNet (without RProvider) then convert them to NA's using a series of RDotNet engine.evaluate statements.

This isn't exactly elegant, so what I'd like to do is streamline the process by taking advantage of RProvider's syntax, e.g.

define a function

    let nan2na (arr: RDotNet.SymbolicExpression) =
        engine.SetSymbol("arr",arr)
        engine.Evaluate("arr[is.nan(arr)] <- NA") |> ignore
        engine.GetSymbol("arr")
    // then we can do the following
    let result = data |> R.data_matrix |> nan2na |> R.fancystuff_etc

However, when I try to do this I get a bunch of nulls back... along with A first chance exception of type 'System.ArgumentException' occurred in RDotNet.dll

What is the best way of dealing with this?

kevinykuo avatar Oct 04 '13 17:10 kevinykuo

I'm a little bit unclear what you are trying to achieve here. But I think in general we don't have good handling for NA in the RProvider. Seems like NaNs are working correctly, but probably NAs are not.

If you have to resort to using evaluate (or anything on the R engine) then we have probably failed. Rather, we should have a way of representing NA on the F# side, so you can map your NaNs to NA on the F# side of things.

The natural way of doing this is probably using the Option type (so a sequence of Option would get converted into a numeric vector, where the None values are NA), or perhaps using Nullable?

Do you think that would work for you?

hmansell avatar Oct 04 '13 19:10 hmansell

Being able to "map to NA on the F# side of things" would be great, and I agree that using options makes sense.

I'm relatively new to F# (coming from R), but the alternatives I can think of are quite messy...

kevinykuo avatar Oct 04 '13 20:10 kevinykuo

This probably depends on what R.NET provides (I think it always returns N.A. as Double.NaN), but it would be good to investigate and provide some option.

tpetricek avatar May 21 '14 18:05 tpetricek