2017-07-04 43 views
0

我想爲我的數據庫的每一行計算基尼指數。每一行都是客戶,每一列都是一個月度會話。因此,我需要做的是在整個12個月內爲每位客戶逐行添加一個包含基尼指數的列。 See example attachedR的基尼指數

我發現了一些例子在線這樣做:

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) 

不過,我得到以下錯誤:

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

我已經安裝了Ineq和Reldist(和庫),所以我不知道爲什麼這不起作用。

+0

從您的代碼我不能重現錯誤,所以要麼你沒有安裝或正確加載軟件包? – agenis

回答

0

嘗試這樣做的列有你的基尼係數_:

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") 

如果爲每個行試試這個希望:

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你的末尾加上coeffs你的data_frame與合併例如:

your_data_final = merge(your_data,data_coeff, by = "customer")