Возможное решение с data.table :
library(data.table)
melt(setDT(df1), id = 1,
measure.vars = patterns(c("^ID","^Time")),
value.name = c("ID","Time")
)[df2, on = .(ID), Cost := Time * i.Cost
][, dcast(.SD, Branch ~ variable, value.var = c("ID","Time","Cost"))]
, что дает:
Branch ID_1 ID_2 ID_3 Time_1 Time_2 Time_3 Cost_1 Cost_2 Cost_3
1: Maine BQQ BKP OLW 11.0 8.1 3.0 16.500 97.119 89.970
2: NYork DDP ADD BQQ 2.0 6.5 0.4 17.980 29.250 0.600
3: Texas BKP LMG DDP 5.5 2.8 8.9 65.945 69.972 80.011
Использованные данные:
df1 <- structure(list(Branch = c("Texas", "Maine", "NYork"), ID.1 = c("BKP", "BQQ", "DDP"), Time.1 = c(5.5, 11, 2),
ID.2 = c("LMG", "BKP", "ADD"), Time.2 = c(2.8, 8.1, 6.5), ID.3 = c("DDP", "OLW", "BQQ"), Time.3 = c(8.9, 3, 0.4)),
.Names = c("Branch", "ID.1", "Time.1", "ID.2", "Time.2", "ID.3", "Time.3"), class = "data.frame", row.names = c(NA, -3L))
df2 <- structure(list(ID = c("ADD", "BKP", "BQQ", "DDP", "LMG", "OLW"), Cost = c(4.5, 11.99, 1.5, 8.99, 24.99, 29.99)),
.Names = c("ID", "Cost"), class = "data.frame", row.names = c(NA, -6L))