How to find top quartile & bottom quartile in R?

479 views Asked by At

Using the mtcars dataset, find car(s) within the top quartile in MPG, bottom quartile in weight, and 5 gears.

I know the answer it just codes not fitting in.

data.frame(mtcars)

mtcars %>% filter(gear>=5)

mtcars[mtcars$wt == min(mtcars$wt), "wt", drop = FALSE]

pls suggest

1

There are 1 answers

0
jpenzer On BEST ANSWER

First extract the max value of the bottom quartile for weigt, and the min value of the upper quartile for mpg, then filter for all 3 conditions.

wt_lower <- quantile(mtcars$wt)[[2]]
mpg_higher <- quantile(mtcars$mpg)[[4]]
mtcars %>%
  filter(wt < wt_lower,
         mpg > mpg_higher,
         gear == 5)