Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 %>% 
group_by(Species)%>% 
summarise(n = n())

Number 2) I can get by this code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

iris %>% 
group_by(Species)%>% 
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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading