我是R编程的新手,完全坚持为下面的问题寻找解决方案。
我有一个数据集' full_data'(接近80个变量),但简短如下:
CustomerID ReachRatio CustomerGrade PolicyCount
1 10 Loyal 2
2 40 Normal 6
3 80 VIP 11
4 100 Normal 7
CustomerID: sequence of unique ID
Reach :a score out of 100 for customer based on contact details
CustomerGrade: It has label as 'Normal','VIP','Loyal' or 'To be calculated','NA' and 'Uncalculated' etc
PolicyCount:No of policy brought by customer in a timeframe so >5 is good
我想在r中编写一个函数,根据权重为这3个客户计算得分: / *此代码无效* /
full_data$CustomerScore = apply(full_data,1,function(row)
(((ifelse(row["CustomerGrade"]=='LOYAL',1,0)*30)+
(ifelse(row["CustomerGrade"]=='NORMAL',1,0)*20)+
(ifelse(row["PolicyCount"]>=4){ 1*30})+
(ifelse(row["ReachRatio"]>=40 && row["ReachRatio"]<=80,1,0)*40)))
)
所以我的最终结果例如,根据应用于每个类别的权重,客户评分为100分。在上述代码中客户等级:总重量:30(如果忠诚 - 30,正常 - 20,否则 - 0) 政策计数:重量:30 [如果elobrated可以有更多的值,但总重量是30] 到达率重量:40 [例如,如果> 80--40,> 40&amp;&amp; &LT; 80--20 ...]
如何在R?
中有效实施欢迎任何建议和想法!!
非常感谢!!
答案 0 :(得分:0)
我们不需要遍历行。这可以是矢量化的。基于OP的ifelse
陈述
with(df1, sum(30*(CustomerGrade =='Loyal')+
20*(CustomerGrade == 'Normal') +
30*(PolicyCount >=4) +
40*(ReachRatio>=40 & ReachRatio <=80)))
答案 1 :(得分:0)
试试这个:
apply(X = df,MARGIN = 1,function(row){
ifelse(row["CustomerGrade"]=='Loyal',30,0)+
ifelse(row["CustomerGrade"]=='Normal',20,0)+
ifelse(row["PolicyCount"]>=4,30,0)+
ifelse(row["ReachRatio"]>=40 && row["ReachRatio"]<=80,20,0)+
ifelse(row["ReachRatio"]>80,40,0)})
#[1]30 70 40 50