How do I find the index number of the 1st quartile, median and 2nd quartile? I have a data set containing 1006 observations. I could easily find the position of the minimum and maximum value by using
match(min(sp500_logreturns), as.numeric(sp500_logreturns))
But it just spits out NA if i use quantile(sp500_logreturns, 0.25)
and 0.5
and 0.75
instead of min(sp500_logreturns)
in the match
-function... How could I else find the index number?
Possibly even simpler than using match
together with a calculation of quantile
is to simply select the element at the (fractional) position you want after ordering:
Test data:
sp500_logreturns=sample(1:100,size=10006,replace=T)
For first quartile (0.25):
firstq_index=order(sp500_logreturns)[length(sp500_logreturns)*0.25]
# check:
mean(sp500_logreturns<sp500_logreturns[firstq_index])
# [1] 0.2467519
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.