关于数据框:R数据框的重塑,重组和/或合并

R data frame reshape, restructure, and/or merge

我正在尝试根据data.frame中包含的值重塑和"扩展" data.frame。以下是我开始使用的数据帧的结构:

起始结构:

1
2
3
4
5
6
'data.frame':   9 obs. of  5 variables:
 $ Delivery.Location    : chr "Henry""Henry""Henry""Henry" ...
 $ Price                : num  2.97 2.96 2.91 2.85 2.89 ...
 $ Trade.Date           : Date, format:"2012-01-03""2012-01-04""2012-01-05""2012-01-06" ...
 $ Delivery.Start.Date  : Date, format:"2012-01-04""2012-01-05""2012-01-06""2012-01-07" ...
 $ Delivery.End.Date    : Date, format:"2012-01-04""2012-01-05""2012-01-06""2012-01-09" ...

此价格数据所来自的市场称为"第二天市场",因为天然气的实物交割通常是在交易天然气后的第二天(即上述Trade.Date)。我通常会强调一下,因为周末和节假日会有例外,在这种情况下,交货期可能是多天(即2-3天)。但是,数据结构提供有明确声明Delivery.Start.DateDelivery.End.Date的变量。

我正在尝试以以下方式重组data.frame,以生成一些时间序列图并进行其他分析:

所需结构:

1
2
3
4
$ Delivery.Location
$ Trade.Date
$ Delivery.Date    <<<-- How do I create this variable?
$ Price

如何基于现有的Delivery.Start.DateDelivery.End.Date变量创建Delivery.Date变量?

换句话说,来自2012年1月6日Trade.Date的数据如下:

1
2
Delivery Location   Price      Trade.Date      Delivery.Start.Date     Delivery.End.Date    
Henry               2.851322    2012-01-06     2012-01-07              2012-01-09

我想以某种方式"填写" Delivery.Location


您可以使用plyr包中的ddply

1
2
3
4
5
6
7
8
9
10
11
12
13
library(plyr)
ddply(
      df,
      c("Delivery.Location","Trade.Date"),
      function(trade)
      data.frame(
      trade,
      Delivery.Date=seq(
          from=trade$Delivery.Start.Date,
          to=trade$Delivery.End.Date,
          by="day")
      )
 )

当然,您仍然必须实施有关周末,假期等的逻辑。

我还假设Delivery.LocationTrade.Date足以识别单个交易。


可以吗?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
library(plyr)  



lookuptable<-df[,2:3]

Trade.Date<-df[,4]
filluptable1<-as.data.frame(Trade.Date)
Trade.Date<-df[,5]
filluptable2<-as.data.frame(Trade.Date)

myfillstart<- join(filluptable1, lookuptable, by ="Trade.Date")
myfillstart<- rename(myfillstart, c(Trade.Date="Delivery.Start.Date"))
myfillstart<- rename(myfillstart, c(Price="Price.Start.Date"))
myfillend<- join(filluptable2, lookuptable, by ="Trade.Date")
myfillend<- rename(myfillend, c(Trade.Date="Delivery.End.Date"))
myfillend<- rename(myfillend, c(Price="Price.End.Date"))
finaldf<-cbind(df[,1:3],myfillstart,myfillend)



finaldf
    Delivery.Location    Price Trade.Date Delivery.Start.Date Price.Start.Date Delivery.End.Date Price.End.Date
35              Henry 2.965398 2012-01-03          2012-01-04         2.959077        2012-01-04       2.959077
150             Henry 2.959077 2012-01-04          2012-01-05         2.906436        2012-01-05       2.906436
263             Henry 2.906436 2012-01-05          2012-01-06         2.851322        2012-01-06       2.851322
377             Henry 2.851322 2012-01-06          2012-01-07               NA        2012-01-09       2.890364
493             Henry 2.890364 2012-01-09          2012-01-10         2.965585        2012-01-10       2.965585
607             Henry 2.965585 2012-01-10          2012-01-11         2.807734        2012-01-11       2.807734
724             Henry 2.807734 2012-01-11          2012-01-12         2.702072        2012-01-12       2.702072
838             Henry 2.702072 2012-01-12          2012-01-13         2.671732        2012-01-13       2.671732
955             Henry 2.671732 2012-01-13          2012-01-14               NA        2012-01-17             NA

注意:由于您的位置相同,因此我没有查找该位置。但是,您可以执行相同操作。该代码看起来有些混乱。这是您也可以尝试的替代方法。