[R] Function Gini or Ineq

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Fri Sep 3 17:54:06 CEST 2010


for the Gini coefficient you can use this function:

gini <- function(x, unbiased = TRUE, na.rm = FALSE){
     if (!is.numeric(x)) {
         warning("'x' is not numeric; returning NA")
         return(as.numeric(NA))
     }
     if (any(na.ind <- is.na(x))) {
         if (!na.rm)
             stop("'x' contain NAs")
         else
             x <- x[!na.ind]
     }
     n <- length(x)
     mu <- mean(x)
     N <- if (unbiased) n*(n - 1) else n*n
     ox <- x[order(x)]
     dd <- drop(crossprod(2 * 1:n - n - 1,  ox))
     dd / (mu * N)
}

x <- c(541, 1463, 2445, 3438, 4437, 5401, 6392, 8304, 11904, 22261)
G <- gini(x)


I hope it helps.

Best,
Dimitris


On 9/3/2010 5:37 PM, Mestat wrote:
>
> Hi listers,
> Does it necessary to install any package in order to use the GINI or INEQ
> functions.
> If I use the following command the R tells me that didn't find the GINI
> function.
>
> x<-c(541, 1463, 2445, 3438, 4437, 5401, 6392, 8304, 11904, 22261)
> G<-gini(x)
>
> Thanks in advance,
> Marcio

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014



More information about the R-help mailing list