Combine dplyr::group_by with dplyr::summarise and dplyr::summarise_if in a single step

I would like to combine the summarise_if statement (summarise all numeric variables) with the summarise to count the amount of observations.
In the iris example, I would like to

  1. count the number of observations per Species and add this count as a column in the new table
  2. summarise all numeric variables (Sepal.Length,Sepal.Width, Petal.Length, Petal.Width) by Species.

Number 1) I can get by this code:

iris %>% 
summarise(n = n())

Number 2) I can get by this code:

iris %>% 
summarise_if(is.numeric, median, na.rm = TRUE)  

But I am struggling with combining both. Just pipeing one after the other does give me a different result. My desired output would be this:
enter image description here

>Solution :

Use across:

iris %>%
  group_by(Species) %>%
  summarise(n = n(), across(where(is.numeric), median, na.rm = TRUE))

Leave a Reply