我想完成这个:
df <- data.frame(val1 = c(10,11, 12, 13))
df2 <- data.frame(c1 = 1, c2 = 2, c3 = 3, c4 = 4)
df <- data.frame(rep(df, NCOL(df2)))
df2 <- df2[rep(1, NROW(df)),]
df3 <- df + df2
df3
val1 val1.1 val1.2 val1.3
1 11 12 13 14
2 12 13 14 15
3 13 14 15 16
4 14 15 16 17
首先,我想知道是否有更简单的方法。
秒。
不同的df可能是
h <- temp[EM.Names[[i]]]
> head(h)
MSCILATAM
1 9.870000e-03
2 -6.286546e-05
3 1.035069e-02
4 1.070432e-02
5 5.072980e-03
6 1.486852e-03
和另一个df2可能是
> fixed.avg.yield.diff
VENEZUELA PERU COLOMBIA MEXICO BRAZIL ARGENTINA CHILE
0.0037480080 0.0004009513 0.0034571043 -0.0014477117 0.0280813115 0.0006466359 -0.0000884484
> class(h)
[1] "data.frame"
> > class(fixed.avg.yield.diff)
[1] "numeric"
在这种情况下,上述解决方案将无效。原因是如果我尝试转换&#34; fixed.avg.yield.diff&#34;对于data.frame,它的结构如下:
> data.frame(fixed.avg.yield.diff)
fixed.avg.yield.diff
VENEZUELA 0.0037480080
PERU 0.0004009513
COLOMBIA 0.0034571043
MEXICO -0.0014477117
BRAZIL 0.0280813115
ARGENTINA 0.0006466359
CHILE -0.0000884484
答案 0 :(得分:0)
这样的东西?
outer(unlist(df),unlist(df2),"+")
# c1 c2 c3 c4
# val11 11 12 13 14
# val12 12 13 14 15
# val13 13 14 15 16
# val14 14 15 16 17
使用您的其他示例,
outer(unlist(h),unlist(fixed.avg.yield.diff),"+")
# VENEZUELA PERU COLOMBIA MEXICO BRAZIL ARGENTINA CHILE
# MSCILATAM1 0.013618008 0.0102709513 0.013327104 0.0084222883 0.03795131 0.0105166359 0.0097815516
# MSCILATAM2 0.003685143 0.0003380858 0.003394239 -0.0015105772 0.02801845 0.0005837704 -0.0001513139
# MSCILATAM3 0.014098698 0.0107516413 0.013807794 0.0089029783 0.03843200 0.0109973259 0.0102622416
# MSCILATAM4 0.014452328 0.0111052713 0.014161424 0.0092566083 0.03878563 0.0113509559 0.0106158716
# MSCILATAM5 0.008820988 0.0054739313 0.008530084 0.0036252683 0.03315429 0.0057196159 0.0049845316
# MSCILATAM6 0.005234860 0.0018878033 0.004943956 0.0000391403 0.02956816 0.0021334879 0.0013984036