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

Asked

Viewed 86 times

3

In the execution of loops with functions apply, is always used function(x). Example:

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

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

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

I always wear them, but I don’t understand them clearly.

What is the logic of using these functions?

1 answer

3


The function lapply takes two arguments - the first is a list (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 the first argument.

Therefore, we could create a function and then pass it to the 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 that’s what we do when we pass:

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

Like the lapply will pass each element of the list as argument of this function, only one parameter will be changed, so in general we use only functions with only one argument.

Already the Reduce uses a binary function because 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 argument is irrelevant, it could be any name, since both the lapply as to the Reduce use the order of arguments.

  • 1

    Good answer but could complete with one or another example of functions with more than one argument used with lapply. Cases such as lapply(lista, mean, na.rm = TRUE) but with anonymous or user-defined functions.

  • 2

    Hi Rui, feel free to include one more reply with more examples. I also corrected function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.