给出两个数据帧:
d1=structure(list(y = c(0.04090403771224, 0.321216286364446, -1.00056198338576,
0.549872767053012, 0.746529361891068, -0.756394989312306, 0.432210041946058,
1.04202671889042, 0.846691694527378, -0.0372890199537169), x = c(-0.626453810742332,
0.183643324222082, -0.835628612410047, 1.59528080213779, 0.329507771815361,
-0.820468384118015, 0.487429052428485, 0.738324705129217, 0.575781351653492,
-0.305388387156356), tx = c(1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L,
1L), x2 = c(-0.41499456329968, -0.394289953710349, -0.0593133967111857,
1.10002537198388, 0.763175748457544, -0.164523596253587, -0.253361680136508,
0.696963375404737, 0.556663198673657, -0.68875569454952)), .Names = c("y",
"x", "tx", "x2"), row.names = c(NA, -10L), class = "data.frame")
d2=dput(reg1.coefs)
structure(c(-0.752515009279226, 2.43055896098665, 0.833724197554561,
-1.79389056223944), .Names = c("(Intercept)", "x", "tx", "x:tx"
))
如何创建第三个数据框,仅选择d2
中的变量,并在d2
中使用':'创建与变量的乘积相对应的其他变量。在此示例中,代码应返回与x:tx
中x
和tx
的乘积相对应的第三个变量d
。
out=structure(list(y = c(0.04090403771224, 0.321216286364446, -1.00056198338576,
0.549872767053012, 0.746529361891068, -0.756394989312306, 0.432210041946058,
1.04202671889042, 0.846691694527378, -0.0372890199537169), x = c(-0.626453810742332,
0.183643324222082, -0.835628612410047, 1.59528080213779, 0.329507771815361,
-0.820468384118015, 0.487429052428485, 0.738324705129217, 0.575781351653492,
-0.305388387156356), tx = c(1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L,
1L), x1x2 = c(-0.626453810742332, 0.183643324222082, -0.835628612410047,
1.59528080213779, 0.329507771815361, -0.820468384118015, 0, 0,
0.575781351653492, -0.305388387156356)), .Names = c("y", "x",
"tx", "x1x2"), row.names = c(NA, -10L), class = "data.frame")
对于一个案例来说这显然很容易,但我需要代码足够通用,以便d2
包含其他变量的产品(例如x:x2
或x:x2:tx
)正确out
已生成。