Reputation: 1057
I have data (e.g.d and di) that I want to do some calculations.
data:
d= 1:20
xi= 2:21
I would like to use this equation(using the 95th and 5th percentiles separately for both d and xi):
res= d(95th)-d(5th)/xi(95th)+xi(5th)
res2= res + xi(5th)
we get 95th and 5th percentiles using this function(provided by users of stackoverflow):
fun <- function(x){
quantiles <- quantile( x, c(.05, .95 ) )
x[ x < quantiles[1] ] <- quantiles[1]
x[ x > quantiles[2] ] <- quantiles[2]
x
}
but this does not apply to my case as I want to use the 95th and 5th percentiles separately in the equation above.any ideas please.
Upvotes: 0
Views: 2254
Reputation: 66819
Is this what you are looking for?
your_fun <- function(x,y){
qx <- quantile(x,c(.05,.95))
qy <- quantile(y,c(.05,.95))
out <- diff(qx)/diff(qy)
out2 <- out + qy[1]
names(out) <- NULL
names(out2) <- NULL
list(res=out,res2=out2)
}
your_fun(d,xi)
which gives
$res
[1] 1
$res2
[1] 3.95
Upvotes: 3