'How to find top quartile & bottom quartile in R?

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



Solution 1:[1]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jpenzer