do.call for all dataframe columns with lazy evaluation and dplyr function

214 Views Asked by At

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.

1

There are 1 best solutions below

1
On BEST ANSWER

do.call is used to supply multiple function arguments to a single execution of a function. For example, instead of writing paste('c', 1:2) we can use a list of arguments such that do.call(paste, list('c', 1:2)) gives the same result.

So in your case, do.call is the same as running stats(col1, col2, col3, ...). You can easily see that this won't work, since stats 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:

lapply(names(data), stats)