Home > Software design >  Count records below the 30% percentile
Count records below the 30% percentile

Time:04-04

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 ) ))

CodePudding user response:

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

CodePudding user response:

In base R you may do

num <- sapply(mtcars, is.numeric)
sapply(mtcars[num], \(x) sum(x < quantile(x, .3)))
# mpg  cyl disp   hp drat   wt qsec   vs   am gear carb 
#  10    0   10   10    9   10    9    0    0    0    7 

or

colSums(mtcars[num] < lapply(mtcars[num], quantile, .3))
# mpg  cyl disp   hp drat   wt qsec   vs   am gear carb 
#  10    0   10   10    9   10    9    0    0    0    7 

or (using matrixStats::colQuantiles)

colSums(mtcars[num] < as.list(matrixStats::colQuantiles(as.matrix(mtcars[num]), probs=.3)))
# mpg  cyl disp   hp drat   wt qsec   vs   am gear carb 
#  10    0   10   10    9   10    9    0    0    0    7 
    
  • Related