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.