计算R中多天的差异

时间:2015-12-06 11:06:35

标签: r merge diff

我有一个名为crsp1的表,如下所示:

structure(list(NDATE = structure(c(5611, 5543, 5241, 5948, 5835, 
5969, 5466, 6019, 5696, 5942, 5159, 5921, 5447, 5920, 5418, 5152, 
5615, 5522, 5649, 5486, 5452, 5684, 5559, 5157, 5408, 5375, 5481, 
5381, 5250, 5356, 5411, 5391, 5233, 5459, 5935, 5271, 5689, 5191, 
5744, 6024, 5496, 5829, 5880, 5144, 5193, 5781, 5796, 5360, 5333, 
5927, 5863, 5503, 5999, 5293, 5376, 5857, 5174, 5122, 5584, 5986, 
5571, 5374, 5390, 5269, 5468, 5888, 5221, 5633, 5186, 5997, 5975, 
5354, 5893, 5513, 5523, 5123, 5506, 5709, 5410, 5685, 5320, 5993, 
5328, 5908, 5380, 5307, 5312, 5979, 5776, 5706, 5830, 5767, 5761, 
5179, 5775, 5184, 5188, 5809, 5467, 5710), class = "Date"), CUSIP = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = "000165100", class = "factor"), PRCCD = c(5.5, 
6, 7.5, 12.5, 10.125, 12.875, 3.75, 15.5, 6.875, 11.875, 6.5, 
12, 3.5, 11.875, 4, 6, 5.875, 5.625, 6, 4.5, 3.625, 6.5, 5.5, 
6.75, 4, 4.75, 3.875, 4.625, 6.75, 4, 4, 4.25, 7.125, 4, 12, 
6.625, 7, 6.25, 6.875, 15, 4.375, 10.25, 10.375, 7.75, 6.25, 
8, 8.25, 4.625, 5, 11.75, 10.125, 4.875, 15, 5.75, 4.5, 10, 6.25, 
7.5, 4.75, 13.25, 5.75, 4.75, 4.25, 6.5, 3.625, 10.375, 6.875, 
6, 6.5, 14.5, 12.75, 5.5, 10.5, 5.25, 5.25, 8.75, 5, 6.75, 4, 
6.5, 4.75, 14.875, 5.25, 11.25, 4.625, 4.875, 4.75, 12.5, 8.125, 
6.75, 10.25, 7.125, 7.125, 6.25, 8.25, 6.5, 6.5, 9.5, 3.75, 6.875
)), .Names = c("NDATE", "CUSIP", "PRCCD"), row.names = c(NA, 
-100L), .internal.selfref = <pointer: 0x00000000001d0788>, class = c("data.table", 
"data.frame"))

我们有另一张表ndata

structure(list(NDATE = structure(c(5333, 5523, 5857, 5781, 5376
), class = "Date"), CUSIP = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "000165100", class = "factor")), .Names = c("NDATE", 
"CUSIP"), row.names = c(NA, -5L), .internal.selfref = <pointer: 0x00000000001d0788>, class = c("data.table", 
"data.frame"))

对于表ndata中的每一行,我想使用来自表{{crsp1,PRCCD(NDATE + 3)/ PRCCD(NDATA-1)的信息创建一个名为returns的新列。 1}}。

我不确定如何有效地做到这一点。我可以用一种非常繁琐的方式做到这一点,但这不是最佳选择。请建议一个好方法。

0 个答案:

没有答案