2016-04-13 4 views
0

別のデータテーブルから算出された情報と名前を使用して列を追加します。R、:プログラム的に次のように私は2つのデータテーブルを持っている

> landing_fees 
     date airport period_a_fee period_b_fee period_c_fee 
1: 20160101  LAX   126   106   138 
2: 20160101  PHX   137   120   177 
3: 20160101  HNL   156   117   192 
4: 20160102  LAX   189   167   120 
5: 20160102  PHX   119   137   163 
6: 20160102  HNL   186   173   112 
7: 20160103  LAX   197   147   125 
8: 20160103  PHX   162   197   136 
9: 20160103  HNL   158   192   101 
> routes 
    origin destination period 
1: LAX   PHX period_a 
2: LAX   HNL period_a 
3: LAX   PHX period_b 
4: LAX   HNL period_c 
5: PHX   LAX period_a 
6: PHX   HNL period_a 
7: PHX   LAX period_b 
8: PHX   HNL period_c 

本当の次の出力として

set.seed(1) 
landing_fees <- data.table(`date`= c(20160101,20160101,20160101, 
      20160102,20160102,20160102,20160103,20160103,20160103), 
      airport=c("LAX","PHX","HNL"), period_a_fee=sample(100:200,9), 
      period_b_fee=sample(100:200,9), period_c_fee=sample(100:200,9)) 
routes <- data.table(origin=c(rep("LAX",4),rep("PHX",4)), 
      destination=c("PHX","HNL","PHX","HNL","LAX","HNL","LAX","HNL"), 
      period=rep(c("period_a","period_a","period_b","period_c"),2)) 

データセットは数百万の起源/目的地の組み合わせ と年月の価値があり、巨大です。 landing_fees $ dateの各固有の日付のルートテーブルに新しい列を追加し、日付と_cost(つまり20160101_cost)で列に名前を付けたいと思います。次に、新しい列に、landing_feesテーブルにリストされているように、起点料金と目的地料金(一致期間を使用)を合計します。たとえば、経路表では、新しい列経路$ 20160101_costが作成され、行1の値は次のようになります。126のLAX period_a料金(landing_fees表から)+ 137(landing_fees表からの)PHX period_a料金は263になります。同じ計算が各日付の起点/目的地/期間の組み合わせごとに行われます。

最終的な出力は次のようになります。データセットはかなり大きくなる可能性が

origin destination period 20160101_cost 20160102_cost 20160103_cost 
1: LAX   PHX period_a 263   308   359 
2: LAX   HNL period_a 282   375   355 
3: LAX   PHX period_b 226   304   344 
4: LAX   HNL period_c 330   232   226 
5: PHX   LAX period_a 263   308   359 
6: PHX   HNL period_a 293   305   320 
7: PHX   LAX period_b 226   304   344 
8: PHX   HNL period_c 369   275   237 

ので、私は効率的data.tableスクリプトを使用するように期待しています。私はWindows 7マシンでdata.tableバージョン1.9.6を使用しています。

答えて

3

data.tableから溶湯とキャストを使用してこれを行うことができます。

これは、マージのためのあなたのコストマトリックスフラット化:

lf_flat <- melt(landing_fees, id.vars = c("date", "airport"), variable.name = "period")[, period := gsub("_fee", "", period)] 

が次にルーティングテーブルをマージし、そして崩壊はあなたに統合テーブルを与える

DT <- merge(routes, lf_flat,by.x = c("origin","period"), by.y = c("airport","period")) 
DT <- merge(DT, lf_flat, by.x = c("destination","period","date"), by.y = c("airport","period","date")) 
DT[, cost := value.x + value.y][,(c("value.x","value.y")) := NULL] 

の費用:

DT 
    destination period  date origin cost 
1:   HNL period_a 20160101 LAX 282 
2:   HNL period_a 20160101 PHX 293 
3:   HNL period_a 20160102 LAX 375 
4:   HNL period_a 20160102 PHX 305 
5:   HNL period_a 20160103 LAX 355 
6:   HNL period_a 20160103 PHX 320 
7:   HNL period_c 20160101 LAX 330 
8:   HNL period_c 20160101 PHX 369 
9:   HNL period_c 20160102 LAX 232 
10:   HNL period_c 20160102 PHX 275 
11:   HNL period_c 20160103 LAX 226 
12:   HNL period_c 20160103 PHX 237 
13:   LAX period_a 20160101 PHX 263 
14:   LAX period_a 20160102 PHX 308 
15:   LAX period_a 20160103 PHX 359 
16:   LAX period_b 20160101 PHX 226 
17:   LAX period_b 20160102 PHX 304 
18:   LAX period_b 20160103 PHX 344 
19:   PHX period_a 20160101 LAX 263 
20:   PHX period_a 20160102 LAX 308 
21:   PHX period_a 20160103 LAX 359 
22:   PHX period_b 20160101 LAX 226 
23:   PHX period_b 20160102 LAX 304 
24:   PHX period_b 20160103 LAX 344 
    destination period  date origin cost 

私は個人的にはこのようにしておきますが、それはあなたが望むマトリックスと同じデータを与えますが、wi柔軟性が向上します。希望する状態にするには、次のようにしてください:

dcast(DT, origin + destination + period ~ date, value.var = "cost") 
    origin destination period 20160101 20160102 20160103 
1: LAX   HNL period_a  282  375  355 
2: LAX   HNL period_c  330  232  226 
3: LAX   PHX period_a  263  308  359 
4: LAX   PHX period_b  226  304  344 
5: PHX   HNL period_a  293  305  320 
6: PHX   HNL period_c  369  275  237 
7: PHX   LAX period_a  263  308  359 
8: PHX   LAX period_b  226  304  344 
関連する問題