Search code examples
rcountquantile

Count records below the 30% percentile


I have a table like

mtcars

I need to count how many models are velow the 30% percentile. I found that perecentile. but I do not found how to count them.

library(dplyr)
library(purrr)
mtcars %>% summarise(across(where(is.numeric), ~ quantile(.x, 0.30 ) ))  
mpg cyl   disp    hp drat    wt  qsec vs am gear carb
1 15.98   4 142.06 106.2 3.15 2.773 17.02  0  0    3    2

mtcars %>% summarise(across(where(is.numeric), ~ quantile(.x, 0.30 ) ))


Solution

  • We convert to a logical vector (.x < quantile(.x, 0.30)) and get the count with sum - as TRUE -> 1 and FALSE -> 0

    library(dplyr)
    mtcars %>% 
        summarise(across(where(is.numeric), ~ sum(.x < quantile(.x, 0.30 ) )))
    

    -output

       mpg cyl disp hp drat wt qsec vs am gear carb
    1  10   0   10 10    9 10    9  0  0    0    7