我正在尝试计算数据库每一行的Gini索引。每行都是客户,每列是每月会话。所以我需要做的是在整个12个月内为每个客户添加一个按行添加Gini的列。 See example attached
我在网上找到了一些例子:
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)
但是,我收到以下错误:
match.fun(FUN)出错:找不到对象'gini'
我已经安装了Ineq和Reldist(和库),所以我不知道为什么这不起作用。
答案 0 :(得分: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")
最后,您可以使用合并在data_frame的末尾添加系数:
your_data_final = merge(your_data,data_coeff, by = "customer" )