make the r function quantile return a numeric value

The quantile function returns something like

22%

86

and not just the number 86 that i can keep calculating with. Taking an example from this website

we have

#creates a vector of values
df<-c(12,3,4,56,78,18,NA,46,78,100,NA)

#returns the 22 and 77th percentiles of the input values
quantile(df,na.rm = T,probs = c(0.22,0.77))

which leads to the output

22%       77% 
10.08     78.00

and not just a numeric vector. For my purposes I just need one value, so no vector is needed. Having said that, how can I implement something like

Cutoff<-quantile(...)

and get Cutoff to be the numeric value needed to cross a certain percentile, so I can then keep calculating with that number in my program?

Hi @Daniel_R. You can turn the quantile output to numeric by adding as.numeric(). Then, if there's a certain cutoff you're trying to achieve, you can limit the output by filtering.

#returns the 22 and 77th percentiles of the input values
my_quants = quantile(df,na.rm = T,probs = c(0.22,0.77)) |> as.numeric()

my_quants
#> [1] 10.08 78.00

cutoff = 25

final = my_quants[my_quants > cutoff]

final
#> [1] 78

Created on 2023-12-02 with reprex v2.0.2

The function is returning numerical values, it's just doing so by default as a named vector.

This requires no additional steps or conversions:

quantile(df ,na.rm = TRUE, probs = c(0.22,0.77), names = FALSE)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.