What is the logic of function (x) and function (x, y) in R?

4

When executing loops with apply functions, function(x) is always used. Example:

lapply(df,function(x)gsub('[^A-Z]','',x))

In other functions, however, function(x,y) is used. Example:

Reduce(function(x,y)merge(x,y,all=TRUE,by='row.names'),list)

I always use them, but I do not understand them clearly.

What is the logic of using these functions?

    
asked by anonymous 21.09.2018 / 20:14

1 answer

3

The function lapply receives two arguments - the first is a list (it can be a data.frame - since it is also a list) and the second argument is a function that will be applied to each element of the list passed as first argument.

So we could create a function and then pass it to lapply , for example:

my_fun <- function(x) gsub('[^A-Z]','',x)
lapply(df, my_fun)

It turns out that in R, functions can be created even without having a name, and this is what we do when we pass:

lapply(df,function(x)gsub('[^A-Z]','',x))

Since lapply will pass each list element as an argument to this function, only one parameter will be changed, so we usually use only functions with only one argument.

While Reduce uses a binary function, it will combine element 1 with element 2 and then the result with element 3 and so on.

It would be the same thing as creating a function:

my_fun2 <- function(x, y) merge(x,y,all=TRUE,by='row.names'),list)
Reduce(my_fun2,list)

In both cases, the name of the arguments is irrelevant, could be any name, since both lapply and Reduce use the order of the arguments.

    
21.09.2018 / 21:28