fslaborg / RProvider

Access R packages from F#
http://fslab.org/RProvider/
Other
235 stars 69 forks source link

Slice R dataframe in F# #150

Closed tinoswe closed 9 years ago

tinoswe commented 9 years ago

Hi all, I am trying to slice an R dataframe in F# but I am not able to find the solution... In the fsx test case that you find below I am trying to get values of column "v1" when "cat" = "A", i.e. 1.14 and 1.13.

Could you please help me? Many thanks in advance.

let v1 = [| "A"; "B"; "A" |]
let v2 = [| 1.14; 1.78; 1.13 |]
let v3 = [| 1.17; 1.12; 1.1 |]

let dataset = namedParams ["cat", box v1;
                            "v1", box v2;
                            "v2", box v3;] |> R.data_frame
//"cat"    "v1"     "v2"
// A      1.14   1.17
// B      1.78   1.12
// A      1.13   1.1```
tpetricek commented 9 years ago

I suppose you could get this to work using the R provider, but it sounds like a problem that would be better solved using the Deedle library, which is an F# implementation of data frames and lets you do something like this:

let v1 = [| "A"; "B"; "A" |]
let v2 = [| 1.14; 1.78; 1.13 |]
let v3 = [| 1.17; 1.12; 1.1 |]

let dataset = 
  frame [ "cat" =?> Series.ofValues v1
          "v1" =?> Series.ofValues v2
          "v2" =?> Series.ofValues v3 ]

dataset
|> Frame.filterRowValues (fun row -> row.GetAs "cat" = "A")

Deedle works nicely with the R provider, so you should be able to pass Deedle frames to R and vice versa.

If you need, for some reason, to do the data processing on the R side, then you'll have to convert the corresponding R code to use the R provider - which should be doable, but harder (if you have specific problem with doing that, then please open an issue with the problem details).

tinoswe commented 9 years ago

Many thanks Tomas. This was very helpful.