0

for the mtcars sample. I want to get the corresponding mpg value for each quantiles of wt. See code below.

> quantile(mtcars$wt, c(.15,.3,.45,.6,.75,.9),na.rm=T)
   15%    30%    45%    60%    75%    90% 
2.1790 2.7730 3.1890 3.4400 3.6100 4.0475

So in the code above I have the .15, .3, .45, .6, .75, and .9 quantiles for mtcars$wt. And I want to get the corresponding values for mtcars$mpg for each quantiles of the mtcars$wt above.

I tried this code but definitely does not work.

mtcars$mpg[quantile(mtcars$wt, c(.15,.3,.45,.6,.75,.9),na.rm=T)]
Bustergun
  • 977
  • 3
  • 11
  • 17

1 Answers1

1

Probably, you are looking for findInterval/cut

breaks <- quantile(mtcars$wt, c(.15,.3,.45,.6,.75,.9))
findInterval(mtcars$wt, breaks)
#[1] 1 2 1 3 4 4 4 3 2 4 4 6 5 5 6 6 6 1 0 0 1 4 3 5 5 0 0 0 2 1 4 2

This gives you where every value of mtcars$wt lies in breaks.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213