简体   繁体   English

在 R 中将具有不同长度和两个条件的不同数据帧的列相乘

[英]Multiplying columns from different dataframes with different lengths and two conditions in R

I have two data frames with different lengths.我有两个不同长度的数据框。 I want to mutate the columns in data frame df with the multiplication of CAP * currency and Go * currency from df_cur .我想用df_cur中的CAP * currencyGo * currency的乘法来改变数据帧df中的列。 This should be done with the conditions that country and Year must be the same for the two data fames respectively.这应该在两个数据名的国家和年份必须相同的条件下完成。 More specifically,进一步来说,

#df# #df#

Country国家 Sector部门 Year Cap Go Go Exposion曝光
AUS澳大利亚 A一个 2000 2000 100 100 200 200 0.2 0.2
AUS澳大利亚 B 2000 2000 150 150 200 200 0.3 0.3
AUS澳大利亚 C C 2000 2000 160 160 160 160 0.25 0.25
AUS澳大利亚 A一个 2001 2001年 110 110 200 200 0.25 0.25
AUS澳大利亚 B 2001 2001年 140 140 190 190 0.4 0.4
AUS澳大利亚 C C 2001 2001年 165 165 155 155 0.2 0.2
BEL贝尔 A一个 2000 2000 50 50 150 150 0.1 0.1
BEL贝尔 B 2000 2000 70 70 160 160 0.15 0.15
BEL贝尔 C C 2000 2000 100 100 200 200 0.2 0.2
BEL贝尔 A一个 2001 2001年 55 55 160 160 0.15 0.15
BEL贝尔 B 2001 2001年 65 65 140 140 0.1 0.1
BEL贝尔 C C 2001 2001年 110 110 190 190 0.3 0.3

#df_cur# #df_cur#

country国家 year currency货币
AUS澳大利亚 2000 2000 0.58 0.58
AUS澳大利亚 2001 2001年 0.60 0.60
BEL贝尔 2000 2000 0.92 0.92
BEL贝尔 2001 2001年 0.95 0.95

So, I want to transform df like:所以,我想像这样转换 df :

#df# #df#

Country国家 Sector部门 Year Cap Go Go Exposion曝光
AUS澳大利亚 A一个 2000 2000 100*0.58 100*0.58 200*0.58 200*0.58 0.2 0.2
AUS澳大利亚 B 2000 2000 150*0.58 150*0.58 300*0.58 300*0.58 0.3 0.3
AUS澳大利亚 C C 2000 2000 160*0.58 160*0.58 160*0.58 160*0.58 0.25 0.25
AUS澳大利亚 A一个 2001 2001年 110*0.6 110*0.6 200*0.6 200*0.6 0.25 0.25
AUS澳大利亚 B 2001 2001年 140*0.6 140*0.6 190*0.6 190*0.6 0.4 0.4
AUS澳大利亚 C C 2001 2001年 165*0.6 165*0.6 155*0.6 155*0.6 0.2 0.2
BEL贝尔 A一个 2000 2000 50*0.92 50*0.92 150*0.92 150*0.92 0.1 0.1
BEL贝尔 B 2000 2000 70*0.92 70*0.92 160*0.92 160*0.92 0.15 0.15
BEL贝尔 C C 2000 2000 100*0.92 100*0.92 200*0.92 200*0.92 0.2 0.2
BEL贝尔 A一个 2001 2001年 55*0.95 55*0.95 160*0.95 160*0.95 0.15 0.15
BEL贝尔 B 2001 2001年 65*0.95 65*0.95 140*0.95 140*0.95 0.1 0.1
BEL贝尔 C C 2001 2001年 110*0.95 110*0.95 190*0.95 190*0.95 0.3 0.3

I reviewed many answers from Multiplying columns of different size of 2 data frames but nothing worked for me.我从2 个数据帧的不同大小的乘列中查看了许多答案,但对我没有任何帮助。

My code sample:我的代码示例:

Country<-c("AUS","AUS","AUS","AUS","AUS","AUS", "BEL", "BEL", "BEL", "BEL", "BEL", "BEL")
Sector<-c("A","B","C","A","B","C","A","B","C","A","B","C")
Year<-c("2000", "2000", "2000", "2001", "2001", "2001", "2000", "2000", "2000", "2001", "2001", "2001")
Cap<-c(100,150,160,110,140,165,50,70,100,55,65,110)
Go<-c(200,200,160,200,190,155,150,160,200,160,140,190)
Exposion<-c(0.2,0.3,0.25,0.25,0.4,0.2,0.1,0.15,0.2,0.15,0.1,0.3)
df<-data.frame(Country,Sector,Year,Cap,Go,Exposion)

country<-c("AUS","AUS", "BEL", "BEL")
Year<-c("200","2001","2000","2001")
currency<-c(0.58, 0.6, 0.92, 0.95)
df_cur<-data.frame(country,Year,currency)

Thank you very much for your time!非常感谢您的宝贵时间!

Welcome Panagiotis.欢迎帕纳吉奥蒂斯。 The easiest is to first combine the two data.frames.最简单的方法是先将两个 data.frames 组合起来。 Then in the second step you can create new columns with mutate() :然后在第二步中,您可以使用mutate()创建新列:

library(dplyr)

df %>%
  left_join(., df_cur) %>%
  mutate(cap2 = Cap * currency) %>%
  mutate(go2 = Go * currency)

Using data.table使用data.table

library(data.table)
setDT(df)[df_cur, c("Cap", "Go") := 
     .(Cap * currency, Go * currency), on = .(Country = country, Year)]

-output -输出

df
#    Country Sector Year    Cap    Go Exposion
# 1:     AUS      A 2000 100.00 200.0     0.20
# 2:     AUS      B 2000 150.00 200.0     0.30
# 3:     AUS      C 2000 160.00 160.0     0.25
# 4:     AUS      A 2001  66.00 120.0     0.25
# 5:     AUS      B 2001  84.00 114.0     0.40
# 6:     AUS      C 2001  99.00  93.0     0.20
# 7:     BEL      A 2000  46.00 138.0     0.10
# 8:     BEL      B 2000  64.40 147.2     0.15
# 9:     BEL      C 2000  92.00 184.0     0.20
#10:     BEL      A 2001  52.25 152.0     0.15
#11:     BEL      B 2001  61.75 133.0     0.10
#12:     BEL      C 2001 104.50 180.5     0.30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM