我有一个我似乎无法解决的问题。 问题如下:我的数据看起来像这样,是data.table:
Code Date Marketvalue CommonEquity FiscalYearEnd
1: 13643N 2001-03-30 44.01 26761 2001-09-30
2: 13643N 2001-04-30 35.21 26761 2001-09-30
3: 13643N 2001-05-31 48.82 26761 2001-09-30
4: 13643N 2001-06-29 31.74 26761 2001-09-30
5: 13643N 2001-07-31 32.01 26761 2001-09-30
6: 13643N 2001-08-31 20.27 26761 2001-09-30
7: 13643N 2001-09-28 15.20 26761 2001-09-30
8: 13643N 2001-10-31 18.14 18618 2002-09-30
9: 13643N 2001-11-30 22.14 18618 2002-09-30
10: 13643N 2001-12-31 18.67 18618 2002-09-30
11: 13643N 2002-01-31 21.34 18618 2002-09-30
12: 13643N 2002-02-28 18.19 18618 2002-09-30
13: 13643N 2002-03-29 19.47 18618 2002-09-30
14: 13643N 2002-04-30 17.34 18618 2002-09-30
15: 13643N 2002-05-31 13.07 18618 2002-09-30
16: 13643N 2002-06-28 7.47 18618 2002-09-30
17: 13643N 2002-07-31 7.47 18618 2002-09-30
18: 13643N 2002-08-30 6.46 18618 2002-09-30
19: 13643N 2002-09-30 5.87 18618 2002-09-30
20: 13643N 2002-10-31 6.67 13728 2003-09-30
21: 13643N 2002-11-29 4.16 13728 2003-09-30
22: 13643N 2002-12-31 3.47 13728 2003-09-30
23: 13643N 2003-01-31 3.36 13728 2003-09-30
24: 13643N 2003-02-28 3.36 13728 2003-09-30
25: 13643N 2003-03-31 2.61 13728 2003-09-30
26: 13643N 2003-04-30 3.04 13728 2003-09-30
27: 13643N 2003-05-30 3.84 13728 2003-09-30
28: 13643N 2003-06-30 4.43 13728 2003-09-30
Code Date Marketvalue CommonEquity FiscalYearEnd
我想做什么:每年的6月t,我想将以t-1结尾的财政年度的普通股与上一个12月的市场价值相匹配。 现在获取上一个12月的市值并不难:我使用n = 6L的shift函数将12月的市值转移到6月,但是如何使用普通股呢? 例如:对于2002年6月,该会计年度的会计年度结束于2002-09-30,但我需要以t-1结尾的会计年度的普通股,在这种情况下为2001-09-30(值是26761,请参见commonequity列)。 有人可以帮我吗?不幸的是,我没有自己的代码来解决这个问题,因为我根本不知道如何解决会计年度结束问题:(
先谢谢。
编辑:
我包括了预期的输出:
Code Date Marketvalue CommonEquity FiscalYearEnd BEMERatio
1: 13643N 2001-03-30 44.01 26761 2001-09-30 NA
2: 13643N 2001-04-30 35.21 26761 2001-09-30 NA
3: 13643N 2001-05-31 48.82 26761 2001-09-30 NA
4: 13643N 2001-06-29 31.74 26761 2001-09-30 NA
5: 13643N 2001-07-31 32.01 26761 2001-09-30 NA
6: 13643N 2001-08-31 20.27 26761 2001-09-30 NA
7: 13643N 2001-09-28 15.20 26761 2001-09-30 NA
8: 13643N 2001-10-31 18.14 18618 2002-09-30 NA
9: 13643N 2001-11-30 22.14 18618 2002-09-30 NA
10: 13643N 2001-12-31 18.67 18618 2002-09-30 NA
11: 13643N 2002-01-31 21.34 18618 2002-09-30 NA
12: 13643N 2002-02-28 18.19 18618 2002-09-30 NA
13: 13643N 2002-03-29 19.47 18618 2002-09-30 NA
14: 13643N 2002-04-30 17.34 18618 2002-09-30 NA
15: 13643N 2002-05-31 13.07 18618 2002-09-30 NA
16: 13643N 2002-06-28 7.47 18618 2002-09-30 1433.369
17: 13643N 2002-07-31 7.47 18618 2002-09-30 NA
18: 13643N 2002-08-30 6.46 18618 2002-09-30 NA
19: 13643N 2002-09-30 5.87 18618 2002-09-30 NA
20: 13643N 2002-10-31 6.67 13728 2003-09-30 NA
21: 13643N 2002-11-29 4.16 13728 2003-09-30 NA
22: 13643N 2002-12-31 3.47 13728 2003-09-30 NA
23: 13643N 2003-01-31 3.36 13728 2003-09-30 NA
24: 13643N 2003-02-28 3.36 13728 2003-09-30 NA
25: 13643N 2003-03-31 2.61 13728 2003-09-30 NA
26: 13643N 2003-04-30 3.04 13728 2003-09-30 NA
27: 13643N 2003-05-30 3.84 13728 2003-09-30 NA
28: 13643N 2003-06-30 4.43 13728 2003-09-30 5365.418
Code Date Marketvalue CommonEquity FiscalYearEnd BEMERatio
我们没有2001年6月的迷人之处,因为我们的数据表中没有2000年12月以来的市场价值,但2002年6月和2003年6月却没有。 基本上:每年6月:将上一个日历年结束的会计年度的普通股除以上一个12月的市值。在此示例中,2002年6月:26761(截至2001年的会计年度的普通权益)除以18.67(2001年12月的市场价值)。
2003年6月:18618(截至2002财政年度的普通股)除以3.47(2002年12月的市场价值)
答案 0 :(得分:1)
这是使用data.table
的一种可能的方法:
#calculate the prev fiscal year end date for self joining later
DT[!is.na(FiscalYearEnd),
PrevFiscalYearEnd := as.Date(vapply(FiscalYearEnd,
function(x) seq(x, by="-1 year", length.out=2L)[2L],
numeric(1L)),
origin="1970-01-01")]
ans <- DT[!is.na(FiscalYearEnd), BEMERatio :=
#get the CommonEquity for each fiscal year end
unique(DT[!is.na(FiscalYearEnd), .(Code, FiscalYearEnd, CommonEquity)])[
#join to find the previous fiscal year end common equity
DT[!is.na(FiscalYearEnd)], on=c("Code", "FiscalYearEnd"="PrevFiscalYearEnd")][,
#calculate ratio (need to make sure data is complete so that shifting by 6 months is robust)
CommonEquity / shift(Marketvalue, 6L)]
]
输出:
Code Date Marketvalue CommonEquity FiscalYearEnd PrevFiscalYearEnd BEMERatio
1: 13643N 2001-03-30 44.01 26761 2001-09-30 2000-09-30 NA
2: 13643N 2001-04-30 35.21 26761 2001-09-30 2000-09-30 NA
3: 13643N 2001-05-31 48.82 26761 2001-09-30 2000-09-30 NA
4: 13643N 2001-06-29 31.74 26761 2001-09-30 2000-09-30 NA
5: 13643N 2001-07-31 32.01 26761 2001-09-30 2000-09-30 NA
6: 13643N 2001-08-31 20.27 26761 2001-09-30 2000-09-30 NA
7: 13643N 2001-09-28 15.20 26761 2001-09-30 2000-09-30 NA
8: 13643N 2001-10-31 18.14 18618 2002-09-30 2001-09-30 760.0398
9: 13643N 2001-11-30 22.14 18618 2002-09-30 2001-09-30 548.1565
10: 13643N 2001-12-31 18.67 18618 2002-09-30 2001-09-30 843.1317
11: 13643N 2002-01-31 21.34 18618 2002-09-30 2001-09-30 836.0200
12: 13643N 2002-02-28 18.19 18618 2002-09-30 2001-09-30 1320.2269
13: 13643N 2002-03-29 19.47 18618 2002-09-30 2001-09-30 1760.5921
14: 13643N 2002-04-30 17.34 18618 2002-09-30 2001-09-30 1475.2481
15: 13643N 2002-05-31 13.07 18618 2002-09-30 2001-09-30 1208.7173
16: 13643N 2002-06-28 7.47 18618 2002-09-30 2001-09-30 1433.3690
17: 13643N 2002-07-31 7.47 18618 2002-09-30 2001-09-30 1254.0300
18: 13643N 2002-08-30 6.46 18618 2002-09-30 2001-09-30 1471.1930
19: 13643N 2002-09-30 5.87 18618 2002-09-30 2001-09-30 1374.4735
20: 13643N 2002-10-31 6.67 13728 2003-09-30 2002-09-30 1073.7024
21: 13643N 2002-11-29 4.16 13728 2003-09-30 2002-09-30 1424.4836
22: 13643N 2002-12-31 3.47 13728 2003-09-30 2002-09-30 2492.3695
23: 13643N 2003-01-31 3.36 13728 2003-09-30 2002-09-30 2492.3695
24: 13643N 2003-02-28 3.36 13728 2003-09-30 2002-09-30 2882.0433
25: 13643N 2003-03-31 2.61 13728 2003-09-30 2002-09-30 3171.7206
26: 13643N 2003-04-30 3.04 13728 2003-09-30 2002-09-30 2791.3043
27: 13643N 2003-05-30 3.84 13728 2003-09-30 2002-09-30 4475.4808
28: 13643N 2003-06-30 4.43 13728 2003-09-30 2002-09-30 5365.4179
Code Date Marketvalue CommonEquity FiscalYearEnd PrevFiscalYearEnd BEMERatio
如果您真的需要六月以外的几个月才能成为NA:
ans[month(Date)!=6L, BEMERatio := NA_real_]
输出:
Code Date Marketvalue CommonEquity FiscalYearEnd PrevFiscalYearEnd BEMERatio
1: 13643N 2001-03-30 44.01 26761 2001-09-30 2000-09-30 NA
2: 13643N 2001-04-30 35.21 26761 2001-09-30 2000-09-30 NA
3: 13643N 2001-05-31 48.82 26761 2001-09-30 2000-09-30 NA
4: 13643N 2001-06-29 31.74 26761 2001-09-30 2000-09-30 NA
5: 13643N 2001-07-31 32.01 26761 2001-09-30 2000-09-30 NA
6: 13643N 2001-08-31 20.27 26761 2001-09-30 2000-09-30 NA
7: 13643N 2001-09-28 15.20 26761 2001-09-30 2000-09-30 NA
8: 13643N 2001-10-31 18.14 18618 2002-09-30 2001-09-30 NA
9: 13643N 2001-11-30 22.14 18618 2002-09-30 2001-09-30 NA
10: 13643N 2001-12-31 18.67 18618 2002-09-30 2001-09-30 NA
11: 13643N 2002-01-31 21.34 18618 2002-09-30 2001-09-30 NA
12: 13643N 2002-02-28 18.19 18618 2002-09-30 2001-09-30 NA
13: 13643N 2002-03-29 19.47 18618 2002-09-30 2001-09-30 NA
14: 13643N 2002-04-30 17.34 18618 2002-09-30 2001-09-30 NA
15: 13643N 2002-05-31 13.07 18618 2002-09-30 2001-09-30 NA
16: 13643N 2002-06-28 7.47 18618 2002-09-30 2001-09-30 1433.369
17: 13643N 2002-07-31 7.47 18618 2002-09-30 2001-09-30 NA
18: 13643N 2002-08-30 6.46 18618 2002-09-30 2001-09-30 NA
19: 13643N 2002-09-30 5.87 18618 2002-09-30 2001-09-30 NA
20: 13643N 2002-10-31 6.67 13728 2003-09-30 2002-09-30 NA
21: 13643N 2002-11-29 4.16 13728 2003-09-30 2002-09-30 NA
22: 13643N 2002-12-31 3.47 13728 2003-09-30 2002-09-30 NA
23: 13643N 2003-01-31 3.36 13728 2003-09-30 2002-09-30 NA
24: 13643N 2003-02-28 3.36 13728 2003-09-30 2002-09-30 NA
25: 13643N 2003-03-31 2.61 13728 2003-09-30 2002-09-30 NA
26: 13643N 2003-04-30 3.04 13728 2003-09-30 2002-09-30 NA
27: 13643N 2003-05-30 3.84 13728 2003-09-30 2002-09-30 NA
28: 13643N 2003-06-30 4.43 13728 2003-09-30 2002-09-30 5365.418
Code Date Marketvalue CommonEquity FiscalYearEnd PrevFiscalYearEnd BEMERatio