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.
Good answer but could complete with one or another example of functions with more than one argument used with
lapply
. Cases such aslapply(lista, mean, na.rm = TRUE)
but with anonymous or user-defined functions.– Rui Barradas
Hi Rui, feel free to include one more reply with more examples. I also corrected
function
.– Daniel Falbel