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

Calculations based on Quartiles (Any percentile)

For example

summary(mtcars$mpg)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  10.40   15.43   19.20   20.09   22.80   33.90 

I need to calculate of mean and standard deviation of "disp" variable of each quartile based on variable "mpg".

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

>Solution :

Specify the breaks with the quantile of ‘mpg’ in cut, use that as grouping variable to summarise the mean and sd of ‘disp’

library(dplyr)
mtcars %>%
     group_by(mpg_grp = cut(mpg, breaks = c(-Inf, quantile(mpg), Inf))) %>% 
     summarise(disp_mean = mean(disp), disp_sd = sd(disp))
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