使用R汇总,排序和计算加权平均值

时间:2018-06-16 05:59:24

标签: r sorting aggregate weighted-average

我有两个数据帧。一个用于发票明细(df_inv),另一个用于针对发票的收集详细信息(df_coll)。 一张发票可能有多个收款/凭证。 发票表有大约30列,现在我们只检查3列进行此计算(发票号,预期金额,到期日) 类似的集合表有多个变量,对于这种情况,我们考虑3列(发票号,凭证日期,贷方金额) PS:一张300美元的发票可以在3个不同日期通过3张优惠券(每张100美元)获得。贷记金额也可能小于或大于预期金额。 根据发票表中的发票号码(唯一),我需要从汇票表中找到相应的凭证,根据凭证日期按升序排序,找到付款延迟(df_coll $ VoucherDate - df_inv $ DueDate ),然后计算每张发票的加权平均值。

df_inv中的

x4,在df_coll中没有相应的条目。因此它将返回NA

加权平均值计算(包含2张付款凭证的1张发票):

((1st pymt amt* 1st delay days)+ (2nd pymt amt* 2nd delay days))/((% of total credited amount)*(expected amount))

下面的示例数据,

发票表(df_inv)

Invoice No  Expected Amount Due Date   
  x1    1400    02-01-2012
  x2    850     20-04-2012
  x3    1300    30-09-2012
  x4    1500    25-01-2013

收藏表(df_coll)

Invoice No  Voucher Date    Credit Amount
      x1    26-11-2012  100
      x2    24-10-2012  200
      x1    11-05-2012  300
      x1    22-08-2013  100
      x2    12-07-2013  500
      x3    30-01-2014  600
      x2    24-06-2012  100
      x3    31-11-2012  700
      x1    29-02-2012  800

1 个答案:

答案 0 :(得分:0)

这是一个仅使用基数R的可能解决方案:

####################  Recreate your input data.frame's   ##################
df_inv <- 
data.frame(InvoiceNo=c("x1","x2","x3","x4"),
           Expected=c(1400,850,1300,1500),
           AmountDueDate=c("02-01-2012","20-04-2012","30-09-2012","25-01-2013"),
           stringsAsFactors=FALSE)
df_coll <- 
data.frame(InvoiceNo=c("x1","x2","x1","x1","x2","x3","x2","x3","x1"),
           VoucherDate=c("26-11-2012","24-10-2012","11-05-2012","22-08-2013",
                         "12-07-2013","30-01-2014","24-06-2012","30-11-2012","29-02-2012"),
           CreditAmount=c(100,200,300,100,500,600,100,700,800),
           stringsAsFactors=FALSE)
df_inv$AmountDueDate <- as.Date(df_inv$AmountDueDate,format='%d-%m-%Y')
df_coll$VoucherDate <- as.Date(df_coll$VoucherDate,format='%d-%m-%Y')
###########################################################################

m <- merge(df_inv,df_coll,by="InvoiceNo",all.x=TRUE,all.y=FALSE)
m$CrdAmntWeighted <- m$CreditAmount *  as.numeric(m$VoucherDate - m$AmountDueDate)
m$TotCredAmnt <- ave(m$CreditAmount,m$InvoiceNo,FUN=sum)
m$TotCrdAmntWeighted <- ave(m$CrdAmntWeighted,m$InvoiceNo,FUN=sum)
m$WeightedAvg <-  m$TotCrdAmntWeighted / ((m$TotCredAmnt / m$Expected) * m$Expected)

final <- m[!duplicated(m$InvoiceNo),c('InvoiceNo','Expected','TotCredAmnt','WeightedAvg')]

> final
   InvoiceNo Expected TotCredAmnt WeightedAvg
1         x1     1400        1300    137.0000
5         x2      850         800    334.8750
8         x3     1300        1300    257.6154
10        x4     1500          NA          NA