我正在尝试以一种特定的方式合并两个数据框。它们与这两个相似:
df1 <- data.frame(Countries = c("Portugal", "Andorra", "Spain", "Portugal", "Portugal", "Portugal", "Spain"),
Year_of_order = c("2015", "2016", "2014", "2016", "2014", "2015", "2015"),
Type_of_order = c("vegetables", "meat", "fruits", "fruits", "meat", "vegetable", "meat")
)
df2 <- data.frame(Names = c("Andorra", "Andorra", "Andorra", "Andorra", "Portugal", "Portugal", "Portugal", "Spain", "Spain", "Spain"),
Year = c("2014", "2015", "2016", "2017", "2014", "2015", "2016", "2014", "2015", "2016"),
Budget_spent = c("452", "444", "429", "456", "854", "876", "833", "102", "104", "96")
)
第二个数据帧是
pivot_longer
的乘积,这就是为什么它看起来有点奇怪。
我想要的是向 df1 添加一个新列,称为
Budget_spent
:其值应该是 df2 中对应的 Year
和 Names
中指示的值。基本上,当 df1 Year_of_order
和 Countries
都匹配 df2 的 Year
和 Names
时,我想要获得 df2 的 Budget_spent
的值。据我了解,我需要进行双条件匹配,但也许我错了。
如果我的解释不清楚,我希望输出如下:
>df1
Countries Year_of_order Type_of_order Budget_spent
1 Portugal 2015 vegetables 876
2 Andorra 2016 meat 429
3 Spain 2014 fruits 102
4 Portugal 2016 fruits 833
5 Portugal 2014 meat 854
6 Portugal 2015 vegetable 876
7 Spain 2015 meat 104
我尝试了很多东西(
match
,merge
,if
,when
,for row
),但收效甚微,主要是因为我什至无法从概念上掌握我应该应用什么流程:
#Here's the most complete attempt, the rest look quite torn apart
df1$Budget_spent <- for (row in df1$Countries) {
match(df1$Countries, df2$Names)+
match(df1$Year_of_order, df2$Year)+
df2$Budget_spent}
#Error message: argument non-numeric for binary operator
如果您提供有关如何解决该问题的任何帮助或指示,我将非常感激。
left_join(df1, df2, join_by(Countries == Names, Year_of_order == Year))
Countries Year_of_order Type_of_order Budget_spent
1 Portugal 2015 vegetables 876
2 Andorra 2016 meat 429
3 Spain 2014 fruits 102
4 Portugal 2016 fruits 833
5 Portugal 2014 meat 854
6 Portugal 2015 vegetable 876
7 Spain 2015 meat 104