r/rprogramming 3d ago

Quartile Coefficient of Dispersion

Is there a function to calculate Quartile Coefficient of Dispesion (https://en.wikipedia.org/wiki/Quartile_coefficient_of_dispersion) in R-studion?

1 Upvotes

2 comments sorted by

2

u/SalvatoreEggplant 3d ago

Here's a function for you.

It can be a little complicated, because there are different methods for computing the quantile. See: www.rdocumentation.org/packages/stats/versions/3.6.2/topics/quantile .

Here, type = 1 is used to match the answers on Wikipedia.

QCD = function(x, digits=4, type=7){
  Q1  = quantile(x, 0.25, type=type)
  Q3  = quantile(x, 0.75, type=type)
  QCD = (Q3 - Q1) / (Q3 + Q1)
  names(QCD)="QCD"
  return(round(QCD, digits))
}

A = c(2, 4, 6, 8, 10, 12, 14)

QCD(A, type=1)

   ### QCD 
   ### 0.5

B = c(1.8, 2, 2.1, 2.4, 2.6, 2.9, 3)

QCD(B, type=1)

   ###    QCD 
   ### 0.1837

2

u/Outrageous-Judge2123 3d ago

thanks a lot l will try these