我有两个data.frames:
第一个是我每天回归的系数:
> parametrosBase
beta0 beta1 beta4
2015-12-15 0.1622824 -0.012956819 -0.04637442
2015-12-16 0.1641884 -0.007914548 -0.06170213
2015-12-17 0.1623660 -0.005618474 -0.05914809
2015-12-18 0.1643263 0.005380472 -0.08533237
2015-12-21 0.1667710 0.003824588 -0.09040071
第二个是:独立(x)变量:
> head(ir_dfSTORED)
ind m h0x h1x h4x beta0_h0x beta1_h1x beta4_h4x
1 2015-12-15 21 1 0.5642792 0.2859359 0 0 0
2 2015-12-15 42 1 0.3606713 0.2831963 0 0 0
3 2015-12-15 63 1 0.2550200 0.2334554 0 0 0
4 2015-12-15 84 1 0.1943071 0.1883048 0 0 0
5 2015-12-15 105 1 0.1561231 0.1544524 0 0 0
6 2015-12-15 126 1 0.1302597 0.1297947 0 0 0
> tail(ir_dfSTORED)
ind m h0x h1x h4x beta0_h0x beta1_h1x beta4_h4x
835 2015-12-21 2415 1 0.006799321 0.006799321 0 0 0
836 2015-12-21 2436 1 0.006740707 0.006740707 0 0 0
837 2015-12-21 2457 1 0.006683094 0.006683094 0 0 0
838 2015-12-21 2478 1 0.006626457 0.006626457 0 0 0
839 2015-12-21 2499 1 0.006570773 0.006570773 0 0 0
840 2015-12-21 2520 1 0.006516016 0.006516016 0 0 0
我想要的是将"parametrosBase"
的beta0列乘以"ir_dfSTORED"
的h0x列,并将结果存储在beta0_h0x列中。其他人也一样:beta1和beta4
我面临的问题是" ind"中的日期。柱。这个乘法必须尊重日期。
因此,一旦我更改"ir_dfSTORED"
中的那一天,我必须更改为"parametrosBase"
中的同一天。
例如:
第一行"parametrosBase"
df是
2015-12-15 0.1622824 -0.012956819 -0.04637442
在2015-12-15
天固定。然后我做了产品。一旦我在2015-12-16
天进入,我将不得不考虑"parametrosBase"
df的第二行。
我该怎么做?
非常感谢。 :)
答案 0 :(得分:2)
也许你应该首先合并两个数据集:
parametrosBase$ind <- rownames(parametrosBase)
df <- merge(ir_dfSTORED,parametrosBase)
df <- within(df,{
beta0_h0x <- beta0*h0x
beta1_h0x <- beta1*h0x
beta4_h0x <- beta4*h0x
})
由于我不知道数据的结构,您可能必须将日期从rownames
转换为日期格式才能使合并生效。使用ind
作为parametrosBase
中日期的名称是使merge
工作的关键,否则您必须指定要合并的变量。