这就是我的data.frame的样子。最右边的列(性能)是我想要的列。
library(data.table)
dt <- fread('
Name FundName SharePrice TotalShares PurchaseDate Performance
John A 10 500 2016-01-01 0%
John A 20 1000 2016-02-01 20%
John A 10 1500 2016-03-01 -25%%
John B 30 500 2016-04-01 -18.18%
John B 60 1000 2016-05-01 4.16%
Tom A 10 500 2016-01-01 0%
Tom A 20 1000 2016-02-01 20%
Tom A 10 1500 2016-03-01 -25%%
Tom B 30 500 2016-04-01 -18.18%
Tom B 60 1000 2016-05-01 4.16%
')
我希望这是有道理的。在尝试计算累积绩效时,我正在努力跟踪两种基金的价格。谢谢你的帮助。
答案 0 :(得分:0)
我将扩展数据以涵盖每个人的所有Date-Fund组合:
dt_skel = dt[, do.call(CJ, c(.SD, unique=TRUE)),
by=Name, .SDcols=c("FundName", "PurchaseDate")]
dt_full = dt[dt_skel, on=names(dt_skel)]
dt_full[ is.na(TotalShares), TotalShares := 0L]
dt_full[ , SharePrice := SharePrice[1L], by=.(Name, FundName, cumsum(!is.na(SharePrice)))]
然后聚合
res = dt_full[!is.na(SharePrice), .(
PurchaseDate,
spent = cumsum(TotalShares*SharePrice),
value = cumsum(TotalShares)*SharePrice
), by=.(Name, FundName)][, .(
value = sum(value),
spent = sum(spent)
), by=.(Name, PurchaseDate)]
Name PurchaseDate value spent
1: John 2016-01-01 5000 5000
2: John 2016-02-01 30000 25000
3: John 2016-03-01 30000 40000
4: John 2016-04-01 45000 55000
5: John 2016-05-01 120000 115000
6: Tom 2016-01-01 5000 5000
7: Tom 2016-02-01 30000 25000
8: Tom 2016-03-01 30000 40000
9: Tom 2016-04-01 45000 55000
10: Tom 2016-05-01 120000 115000
要将性能指标添加到原始事务表:
dt[res, ret := value/spent - 1, on=c("Name, PurchaseDate")]
假设日期总是按月计算,您可以使用
缩小dt_skel
dt_skel = dt[, MaxDate := max(PurchaseDate), by=Name][,
seq(from = PurchaseDate[1L], to =MaxDate[1L], by="month"), by=.(Name, FundName)]
当然,日期应格式化为Date
或IDate
才能生效。