0
votes

I am trying to figure out how to mutate a single column of data by several functions using dplyr. I can do every column:

library(dplyr)

iris %>%
  group_by(Species) %>%
  mutate_all(funs(min, max))

But I don't know how to select one column. I can imagine something like this though this obviously does not run:

iris %>%
  group_by(Species) %>%
  mutate(Sepal.Length, funs(min, max))

I can sort of accomplish this task using do() and a custom function like this:

summary_func = function(x){
  tibble(max_out = max(x),
         min_out = min(x)
  )
}


iris %>%
  group_by(Species) %>%
  do(summary_func(.$Sepal.Length))

However this doesn't really do what I want to do either because it isn't adding to the exist tibble a la mutate.

Any ideas?

2

2 Answers

6
votes

Use mutate_at

iris %>%
   group_by(Species) %>%
   mutate_at("Sepal.Length", funs(min, max))

It takes a character so watch the quotes

-1
votes

Use mutate

iris %>%
  group_by(Species) %>%
  mutate(min = min(Sepal.Length),
         max = max(Sepal.Length))