0

I am trying to calculate the Gini index for each row of my database. Each row is a customer and each column is a monthly session. So what i need to do is to add a column with the Gini index by row, for each customer throughout the 12 months. See example attached

I found some examples online and did this:

Gini_index <- apply(DT_file[,c('sessions_201607_pct','sessions_201608_pct', 'sessions_201609_pct','sessions_201610_pct','sessions_201611_pct','sessions_201612_pct','sessions_201701_pct','sessions_201702_pct','sessions_201703_pct','sessions_201704_pct','sessions_201705_pct','sessions_201706_pct')], 1, gini)

However, I get the following error:

Error in match.fun(FUN) : object 'gini' not found

I have installed both Ineq and Reldist (and libraries) so I don't know why this isn't working.

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • From your code I can't reproduce the error, so either you did not install or load properly the packages? – agenis Jul 04 '17 at 14:14

1 Answers1

1

Try to do this to have your gini's coeff by column :

library(ineq)

coeff= NULL
for (i in colnames(your_data[,-1])){
  coeff= c(coeff,round(ineq(your_data[,i],type = 'Gini'),4))
}

data_coeff = data.frame(cbind(coeff,colnames(your_data[,-1])))
colnames(data_coeff) = c("Coeff","Colnames")

If you want it by for each rows try this :

your_new_data = as.data.frame(t(your_data[,-1]), row.names =T)

colnames(your_new_data) = your_data[,1]

ind = NULL
for (i in colnames(your_new_data)){
  ind = c(ind,round(ineq(your_new_data[,i],type = 'Gini'),4))
}

data_coeff= data.frame(cbind(ind,colnames(your_new_data)))
colnames(data_coeff) = c("Coeff","customer")

Finaly you add your coeffs at the end of your data_frame with a merge for instance :

your_data_final = merge(your_data,data_coeff, by = "customer" )
MBnnn
  • 308
  • 2
  • 13