合并面板时跳转到下一个可用日期

时间:2014-04-26 10:20:19

标签: r date merge panel

我刚才问过一个非常类似的问题。不同之处在于,在只有一家公司的情况之前。现在它是一个面板数据结构。 我想按日期合并两个data.frames。数据包含股票每个交易日的股票数据。活动包含有关公司的新闻。有些新闻是在非交易日公布的,因此今天没有库存数据。例如,04.01.2000公司的一些新闻" A"发表了。我想将这篇文章与NEXT交易日的返回合并,在这种情况下,返回06.01.2000。那么在面板情况合并时如何跳转到下一个交易日呢?

date1 <- c("01.01.2000","02.01.2000","03.01.2000","06.01.2000","07.01.2000","09.01.2000","01.01.2000","02.01.2000","03.01.2000","06.01.2000","07.01.2000","09.01.2000")
ret1 <- c(-2.0,1.1,3,1.4,-0.2, 0.6, 0.1, -0.21, -1.2, 0.9, 0.3, -0.1)
company1 <- c("A","A","A","A","A","A","B","B","B","B","B","B")
df <- data.frame(date1, ret1, company1)
df

#         date1  ret1 company1
# 1  01.01.2000 -2.00        A
# 2  02.01.2000  1.10        A
# 3  03.01.2000  3.00        A
# 4  06.01.2000  1.40        A
# 5  07.01.2000 -0.20        A
# 6  09.01.2000  0.60        A
# 7  01.01.2000  0.10        B
# 8  02.01.2000 -0.21        B
# 9  03.01.2000 -1.20        B
# 10 06.01.2000  0.90        B
# 11 07.01.2000  0.30        B
# 12 09.01.2000 -0.10        B

date2 <- c("02.01.2000","03.01.2000","04.01.2000","08.01.2000","05.01.2000","08.01.2000","09.01.2000")
news2 <- c("blabla11", "blabla12","blabla13","blabla14","blabla21","blabla22","blabla23")
company2 <- c("A","A","A","A","B","B","B")

event <- data.frame(date2, news2, company2)
event 

#        date2    news2 company2
# 1 02.01.2000 blabla11        A
# 2 03.01.2000 blabla12        A
# 3 04.01.2000 blabla13        A
# 4 08.01.2000 blabla14        A
# 5 05.01.2000 blabla21        B
# 6 08.01.2000 blabla22        B
# 7 09.01.2000 blabla23        B

输出应如下所示:

#        date2    news2 company2 date1        ret
# 1 02.01.2000 blabla11        A 02.01.2000  1.10
# 2 03.01.2000 blabla12        A 03.01.2000  3.00
# 3 04.01.2000 blabla13        A 06.01.2000  1.40
# 4 08.01.2000 blabla14        A 09.01.2000  0.60
# 5 05.01.2000 blabla21        B 06.01.2000  0.90
# 6 08.01.2000 blabla22        B 09.01.2000 -0.10
# 7 09.01.2000 blabla23        B 09.01.2000 -0.10

1 个答案:

答案 0 :(得分:1)

它与您的other question大致相同。唯一的变化是您必须将“公司,日期”设置为执行连接的关键列(请注意,订单很重要 - 它首先按公司排序,然后按日期排序)。

require(data.table) ## 1.9.2
setDT(df)
setDT(event)
setkey(df, company1, date1)
setkey(event, company2, date2)
df[, date := date1]
df[event, roll=-Inf]

   company1      date1 ret1       date    news2
1:        A 02.01.2000  1.1 02.01.2000 blabla11
2:        A 03.01.2000  3.0 03.01.2000 blabla12
3:        A 04.01.2000  1.4 06.01.2000 blabla13
4:        A 08.01.2000  0.6 09.01.2000 blabla14
5:        B 05.01.2000  0.9 06.01.2000 blabla21
6:        B 08.01.2000 -0.1 09.01.2000 blabla22
7:        B 09.01.2000 -0.1 09.01.2000 blabla23