Search code examples
rf#interoptype-providers

(F# RProvider) mean giving a weird result - in some cases


I am starting to use the RProvider. For starters, I have just tried to evaluate functions in different ways. It seems I have already run into problems (perhaps a problem with my understanding of how the RProvider works). I have run the same function in four different ways, which I thought to be equivalent. However, the four example provides me with two different results.

R.sapply(R.c(1,2,3,4,5), R.eval(R.parse(text="mean"))).GetValue<float[]>() 
// val it : float [] = [|1.0; 2.0; 3.0; 4.0; 5.0|]
R.sapply(R.c(1,2,3,4,5),"mean").GetValue<float[]>() 
// val it : float [] = [|1.0; 2.0; 3.0; 4.0; 5.0|]
R.mean(R.c(1,2,3,4,5)).GetValue<float[]>() 
// val it : float [] = [|3.0|]
R.eval(R.parse(text="mean(c(1,2,3,4,5))")).GetValue<float[]>() 
// val it : float [] = [|3.0|]

Can anyone tell me why this is? My own guess is that R.sapply applies the given function element-wise. But how do I get around this?


Solution

  • do.call() is the function in R for "applying" a function to a list of parameters (a slightly different meaning from applying or mapping a function over a vector or list of values, which is what the *apply family does).

    The R function for what you want would be

    do.call("mean",list(c(1,2,3,4,5)))
    

    According to the comments (I don't speak F# myself), the F# analogue would be:

    R.do_call("mean", R.list(R.c(1,2,3,4,5)))