0

Recently I got to know that the map function from purrr package is really powerful and tried to find out how to use it in the following case:

Using iris dataset and using purrr::map function, calculate max, mean, min for all variables Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, respectively for each Species (setosa, versicolor, virginica). Then put the results into a list having

  • a character: Species name, and
  • four vectors: max, mean, min for Sepal.Length, Sepal.Width, Petal.Length, Petal.Width.

Any suggestions? I was using dplyr::mutate, but the result format is not what I want.

iris %>% group_by(Species) %>% summarise(MinSL=min(Sepal.Length), MaxSL=max(Sepal.Length), MeanSL=mean(Sepal.Length), MinPL=min(Petal.Length), MaxPL=max(Petal.Length), MeanPL=mean(Petal.Length)) 

It will be also nice see if there is a solution using dpylr to do the task. Thank you!

1
  • 2
    Why not take a look at the purrr tutorial? There are plenty of examples. Commented Apr 20, 2018 at 9:11

1 Answer 1

1

You don't need purrr. Try:

iris %>% group_by(Species) %>% summarise_at(vars(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width), c("min", "max", "mean")) 

Output

# A tibble: 3 x 13 Species Sepal.Length_min Sepal.Width_min Petal.Length_min Petal.Width_min <fct> <dbl> <dbl> <dbl> <dbl> 1 setosa 4.30 2.30 1.00 0.100 2 versicolor 4.90 2.00 3.00 1.00 3 virginica 4.90 2.20 4.50 1.40 # ... with 8 more variables: Sepal.Length_max <dbl>, Sepal.Width_max <dbl>, # Petal.Length_max <dbl>, Petal.Width_max <dbl>, Sepal.Length_mean <dbl>, # Sepal.Width_mean <dbl>, Petal.Length_mean <dbl>, Petal.Width_mean <dbl> 
Sign up to request clarification or add additional context in comments.

1 Comment

You could even use summarise_allas the summarising function is not applied to the grouping variable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.