How to use dplyr for programming
Asked Answered
W

2

8

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?

Warwickshire answered 31/1, 2014 at 2:51 Comment(2)
See stackoverflow.com/questions/21295936Crudity
we'll get there: github.com/hadley/dplyr/issues/178Bullen
B
4

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

Burgle answered 11/7, 2014 at 20:39 Comment(0)
P
1

This can be done in base R like this:

cars.scaled <- as.data.frame(scale(cars))

or

cars.scaled <- replace(cars, TRUE, lapply(cars, scale))

or

cars.scaled <- cars
cars.scaled[] <- lapply(cars, scale)

The first one above can be translated to work with %>% like this:

cars.scaled <- cars %>% scale %>% as.data.frame
Predial answered 11/7, 2014 at 21:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.