How to use dplyr for programming

cran, dplyr, r

Solution

Easy peasy: use `mutate_each(cars, funs(scale))` or `apply(cars, 2, scale)`.

Problem

I like `dplyr` for data manipulation, but I don't understand how to use it for programming. For example, to rescale some variables, we could do: ``` mutate(cars, speed.scaled = scale(speed), dist.scaled = scale(dist)) ``` Very cool. But now suppose I want to write a function that uses `mutate` to scale all variables in a data frame. How do I create the `...` argument? The best thing I can come up with is something like: ``` fnargs <- lapply(names(cars), function(x){call("scale", as.name(x))}) names(fnargs) <- paste0(names(cars), ".scaled") do.call(mutate, c(.data=as.name("cars"), fnargs)) ``` Or is there an alternative interface that is more programming friendly?

Original source

Related problems