将两个数据帧相乘

时间:2021-04-22 07:26:58

标签: r dataframe

如何将两个数据框相乘,它们具有相同的大小和相同的列名:aaa、bbb、ccc、0、1、2、3、...、n 我想乘以列 0, 1, 2, 3, ..., n,另外将每个列乘以 ccc 列中的值并获得一个新的数据框。 我的示例数据框:

| aaa | bbb | ccc | 0 | 1 | 2 | 3 | ... | n |
| --- | --- | --- | - | - | - | - | --- | - |
| EU  | AU  |2    |0,2|0,5|0,7|1  | ... |5  |
| EU  | PL  |3    |0,4|0,9|0,1|2  | ... |3  |
| ..  | ..  |..   |.. |.. |.. |.. | ... |.. |
| GR  | IN  |5    |0,4|0,9|0,1|2  | ... |3  |
| WR  | ES  |4    |0,6|0,3|0,2|4  | ... |1  |

我想执行以下操作:

| aaa | bbb | ccc | 0                 | 1                 |2                  |3              | ... | n    |
| --- | --- | --- | ---               | ---               | ---               | ----          | --- | ---  |
| EU  | AU  |2    |multi (ccc;0,2;0,2)|multi (ccc;0,5;0,5)|multi (ccc;0,7;0,7)|multi(ccc;1;1) | ... |multi(ccc;5;5) |

1 个答案:

答案 0 :(得分:0)

My data frame:
aaa <- c("EU", "EU", "IN", "GR")
bbb <- c("AU", "PL", "IN", "ES")
ccc <- c(1, 2, 3, 4)
h0 <- c(2, 2, 3, 4)
h1 <- c(3, 4, 3, 1)
h2 <- c(3, 5, 5, 5)
#..
#hn

df1 <- data.frame(aaa, bbb, ccc, h0, h1, h2)
df2 <- data.frame(aaa, bbb, ccc, h0, h1, h2)
print (df1)
print (df2)

aaa <- c("EU", "EU", "IN", "GR")
bbb <- c("AU", "PL", "IN", "ES")
ccc <- c(1, 2, 3, 4)
h0 <- c(1*2*2, 2*2*2, 3*3*3, 4*4*4)
h1 <- c(1*3*3, 2*4*4, 3*3*3, 4*1*1)
h2 <- c(1*3*3, 2*5*5, 3*5*5, 4*5*5)
#..
#hn

df3 <- data.frame(aaa, bbb, ccc, h0, h1, h2)
print (df3)