-2

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.

Community
  • 1
  • 1
Prometheus
  • 1,977
  • 3
  • 30
  • 57
  • No links there, I'm afraid. – Axeman Dec 23 '16 at 17:16
  • Why are you using `do.call`? Your function only takes one argument. Are you looking for `lapply` perhaps? Also, you're missing a `}`. – Axeman Dec 23 '16 at 17:17
  • Sorry, just fixed the links. I am using do.call because I want to pass a list of arguments, which are over 1000 column names of a dataframe. Dont know other why how to do this. – Prometheus Dec 23 '16 at 17:20
  • You're aware that `summarize_all` and `mutate_all` might also serve the same purpose without the need for the `lapply`. – Jake Kaupp Dec 23 '16 at 19:37

1 Answers1

0

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)
Axeman
  • 32,068
  • 8
  • 81
  • 94