0

data.frame に含まれる値に基づいて、data.frame を再形成および「拡張」しようとしています。以下は、私が始めているデータフレームの構造です。

開始構造:

'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.DateますDelivery.End.Date

次の方法で data.frame を再構築して、時系列グラフを作成し、追加の分析を実行しようとしています。

望ましい構造:

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

既存の変数と変数Delivery.Dateの両方に基づいて変数を作成するにはどうすればよいですか?Delivery.Start.DateDelivery.End.Date

つまり、2012-01-06 Trade.Date のデータは次のようになります。

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

次のようなものを取得するために、 2012-01-08の Delivery.Location & Price を何とか「記入」したいと思います。

Delivery Location     Price      Trade.Date      Delivery.Date
Henry                 2.851322    2012-01-06     2012-01-07   
Henry                 2.851322    2012-01-06     2012-01-08   <--new record "filled in"
Henry                 2.851322    2012-01-06     2012-01-09   

以下は、私のdata.frameのサブセットの例です:

##--------------------------------------------------------------------------------------------
## sample data
##--------------------------------------------------------------------------------------------
df <- structure(list(Delivery.Location = c("Henry", "Henry", "Henry", "Henry", "Henry", "Henry", "Henry", "Henry", "Henry"), Price = c(2.96539814293754, 2.95907652120467, 2.9064360152398, 2.85132233314846, 2.89036418816388,2.9655845029802, 2.80773394495413, 2.70207160426346, 2.67173237617745),  Trade.Date = structure(c(15342, 15343, 15344, 15345, 15348, 15349, 15350, 15351, 15352), class = "Date"), Delivery.Start.Date = structure(c(15343, 15344, 15345, 15346, 15349, 15350, 15351, 15352, 15353), class = "Date"),  Delivery.End.Date = structure(c(15343, 15344, 15345, 15348, 15349, 15350, 15351, 15352, 15356), class = "Date")), .Names = c("Delivery.Location", "Price", "Trade.Date", "Delivery.Start.Date", "Delivery.End.Date"), row.names = c(35L, 150L, 263L, 377L, 493L, 607L, 724L, 838L, 955L), class = "data.frame")

str(df)

##--------------------------------------------------------------------------------------------   
## create sequence of Delivery.Dates to potentially use
##--------------------------------------------------------------------------------------------
rng <- range(c(range(df$Delivery.Start.Date), range(df$Delivery.End.Date)))
Delivery.Date <- seq(rng[1], rng[2], by=1)

任意の支援または一般的な指示をいただければ幸いです。

4

2 に答える 2

2

パッケージddplyからご利用いただけますplyr

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.Locationと仮定しました。Trade.Date

于 2013-07-12T00:55:38.920 に答える
1

これでいいですか?

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

注:同じ場所にいるため、場所を調べていません。しかし、同じことができます。コードは少し乱雑に見えます。以下は、あなたが通過できる代替手段です。

于 2013-07-12T01:23:03.560 に答える