【发布时间】:2015-11-13 08:05:40
【问题描述】:
我有一个客户贷款数据库,我想对每个 LoanRefID 进行 ddply 汇总:
LoanRefId Tran_Type TransactionAmount
103 11 LoanIssue 1000.0000
104 11 InitiationFee 171.0000
105 11 Interest 59.6729
106 11 AdministrationFee 64.9332
107 11 RaisedClientInstallment 1295.5757
108 11 ClientInstallment 1295.4700
109 11 PaidUp 0.0000
110 11 Adjustment 0.1361
111 11 PaidUp 0.0000
112 12 LoanIssue 3000.0000
113 12 InitiationFee 399.0000
114 12 Interest 94.9858
115 12 AdministrationFee 38.6975
116 12 RaisedClientInstallment 3532.6350
117 12 ClientInstallment 3532.6100
118 12 PaidUp 0.0000
119 12 Adjustment 0.0733
120 12 PaidUp 0.0000
但是,我只想对每个loanID 的某些行求和。具体来说,我只想总结 Tran_Type == "ClientInstallment" 的位置。
我能想到的唯一方法(似乎行不通)是:
> ddply(test, c("LoanRefId"), summarise, cash_in = sum(test[test$Tran_Type == "ClientInstallment","TransactionAmount"]))
LoanRefId cash_in
1 11 4828.08
2 12 4828.08
这不是每个 LoanRefId 的总和,它只是将 Tran_Type == "CLientInstallment" 错误的所有金额相加。
有没有更好的方法来做这个逻辑和?
【问题讨论】: