How to specify the FUN used in () or the corresponding apply () functions

In the by() function, I will use cor (correlation) as FUN . However, I would like to set use="complete.obs" too.

I do not know how to pass this argument in the part FUN = cor .

For instance,

 by(data, INDICES=list(data$Age), FUN=cor) 
+4
source share
4 answers

probably,

 by(data, INDICES=list(data$Age), FUN=cor, use = "complete.obs") 

will work.

by arguments are passed to FUN .

+7
source

If you start looking at various R help files for functions like by , you can start to notice a curious β€œargument” over and over: ... You will see that the ellipsis is specified along with all other function arguments.

This is actually the argument itself. It will collect any other arguments that you pass, and pass them to subsequent functions, named later. The documentation usually indicates which function these arguments will pass.

In this case, in ?by we see the following:

 ... further arguments to FUN. 

This means that any other arguments you pass to by that do not match those listed will be passed to the function that you pass to FUN .

Another common instance can be found in plot , where the documentation lists only two specific arguments: x and y . Then there ... that collects everything else that you go to plot , and give it to methods or par to set the parameters of the graphic parameter.

So, in the example @kohske use = "complete.obs" will be automatically passed to cor , since it does not match any of the other arguments for by .

+7
source

@kohske and @joran give equivalent answers showing the built-in by functions (which are also present in apply and in the plyr family as a whole) to pass additional arguments to the function provided as this is a common application / problem. @Tomas also shows another way of specifying an anonymous function, which is just a function that calls a "real" function with fixed defined parameters. Fixing the parameters of a function call (for efficiently creating a function with fewer arguments) is a general approach, especially in functional programming approaches; in this context it is called currying or a partial application .

 library("functional") by(data, INDICES=list(data$Age), FUN=Curry(cor, use = "complete.obs")) 

This approach can be used when one function does not use arguments ... to "pass through", and you want to specify the only reason an anonymous function is needed to specify specific arguments.

+2
source

In general, you have 2 options:

1) specify the arguments in the calling function ( tapply() or by() in this case). This also works, even if the key argument to fun() not the first:

 fun <- function(arg1, arg2, arg3) { ... } # just to see how fun() looks like tapply(var1, var2, fun, arg1 = something, arg3 = something2) # arg2 will be filled by tapply 

2) you can write your wrapper function (sometimes it is necessary):

 tapply(var1, var2, function (x) { fun(something, x, something2) }) 
+1
source

All Articles