My question is similar to these enter link description here and enter link description here, but my problem is more complex as it requires multiple dplyr operations and lazy evaluation.
This is my function:
stats <- function(col_names){
require("dplyr")
data %>%
group_by_(col_names) %>%
summarise(Count = n()) %>%
mutate(Percent = prop.table(Count)) -> temp
write.csv(temp, file=paste(col_names,".csv",sep="_"))}
Than, I want to pass every column name as an argument with do.call.
colnames <- names(data)
do.call(stats, as.list(col_names))
But I get a common error:
Error in (function (col_names) :
unused arguments ("loans_approved_amount_limit_in_account", "loans_approved_amount_limit_in_ron")
The function works if I enter the column names seperately. But I have to over 1000 columns, and so I need to automate the process.
do.call
is used to supply multiple function arguments to a single execution of a function. For example, instead of writingpaste('c', 1:2)
we can use a list of arguments such thatdo.call(paste, list('c', 1:2))
gives the same result.So in your case,
do.call
is the same as runningstats(col1, col2, col3, ...)
. You can easily see that this won't work, sincestats
only takes one argument. That's why the error you get speaks about unused arguments.What you want to do instead, is run your function multiple times with a single argument. One way to do that is
lapply
: