合并函数复制所有行

Merge function duplicates all rows

好像有很多类似的问题,但我找不到我需要的答案。所以希望有人能够帮助我。

这是我的两个数据框:

    structure(list(Country = c("Albania", "Algeria", "Angola", "Antigua and Barbuda", "Argentina","Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas","Bahrain","Bangladesh", "Barbados", "Belarus", "Belgium", 
"Belize", "Benin", "Bhutan", "Bolivia (Plurinational State of)", 
"Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei Darussalam", 
"Bulgaria", "Burkina Faso", "Burundi", "Cabo Verde", "Cambodia", 
"Cameroon", "Canada", "Central African Republic", "Chad", "Chile", 
"China", "China, Hong Kong SAR", "China, Macao SAR", "Colombia", 
"Comoros", "Congo", "Costa Rica", "Côte d'Ivoire", "Croatia", 
"Curaçao", "Cyprus", "Czechia", "D.R. of the Congo", "Denmark", 
"Djibouti", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", 
"Equatorial Guinea", "Estonia", "Eswatini", "Ethiopia", "Fiji", 
"Finland", "France", "Gabon", "Gambia", "Georgia", "Germany", 
"Ghana", "Greece", "Grenada", "Guatemala", "Guinea", "Guinea-Bissau", 
"Haiti", "Honduras", "Hungary", "Iceland", "India", "Indonesia", 
"Iran (Islamic Republic of)", "Iraq", "Ireland", "Israel", "Italy", 
"Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kuwait", 
"Kyrgyzstan", "Lao People's DR", "Latvia", "Lebanon", "Lesotho", 
"Liberia", "Lithuania", "Luxembourg", "Madagascar", "Malawi", 
"Malaysia", "Maldives", "Mali", "Malta", "Mauritania", "Mauritius", 
"Mexico", "Mongolia", "Montenegro", "Morocco", "Mozambique", 
"Myanmar", "Namibia", "Nepal", "Netherlands", "New Zealand", 
"Nicaragua", "Niger", "Nigeria", "North Macedonia", "Norway", 
"Oman", "Pakistan", "Panama", "Paraguay", "Peru", "Philippines", 
"Poland", "Portugal", "Qatar", "Republic of Korea", "Republic of Moldova", 
"Romania", "Russian Federation", "Rwanda", "Saint Lucia", "Sao Tome and Principe", 
"Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", 
"Singapore", "Slovakia", "Slovenia", "South Africa", "Spain", 
"Sri Lanka", "St. Vincent and the Grenadines", "State of Palestine", 
"Sudan", "Suriname", "Sweden", "Switzerland", "Syrian Arab Republic", 
"Taiwan", "Tajikistan", "Thailand", "Togo", "Trinidad and Tobago", 
"Tunisia", "Turkey", "Turkmenistan", "U.R. of Tanzania: Mainland", 
"Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", 
"United States of America", "Uruguay", "Uzbekistan", "Venezuela (Bolivarian Republic of)", 
"Viet Nam", "Yemen", "Zambia", "Zimbabwe", "Albania", "Algeria", 
"Angola", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", 
"Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", 
"Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bhutan", 
"Bolivia (Plurinational State of)", "Bosnia and Herzegovina", 
"Botswana", "Brazil", "Brunei Darussalam", "Bulgaria", "Burkina Faso", 
"Burundi", "Cabo Verde", "Cambodia", "Cameroon", "Canada", "Central African Republic", 
"Chad", "Chile", "China", "China, Hong Kong SAR", "China, Macao SAR", 
"Colombia", "Comoros", "Congo", "Costa Rica", "Côte d'Ivoire", 
"Croatia", "Curaçao", "Cyprus", "Czechia", "D.R. of the Congo", 
"Denmark", "Djibouti", "Dominican Republic", "Ecuador", "Egypt", 
"El Salvador", "Equatorial Guinea", "Estonia", "Eswatini", "Ethiopia", 
"Fiji", "Finland", "France", "Gabon", "Gambia", "Georgia", "Germany", 
"Ghana", "Greece", "Grenada", "Guatemala", "Guinea", "Guinea-Bissau", 
"Haiti", "Honduras", "Hungary", "Iceland", "India", "Indonesia", 
"Iran (Islamic Republic of)", "Iraq", "Ireland", "Israel", "Italy", 
"Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kuwait", 
"Kyrgyzstan", "Lao People's DR", "Latvia", "Lebanon", "Lesotho", 
"Liberia", "Lithuania", "Luxembourg", "Madagascar", "Malawi", 
"Malaysia", "Maldives", "Mali", "Malta", "Mauritania", "Mauritius", 
"Mexico", "Mongolia", "Montenegro", "Morocco", "Mozambique", 
"Myanmar", "Namibia", "Nepal", "Netherlands", "New Zealand", 
"Nicaragua", "Niger", "Nigeria", "North Macedonia", "Norway", 
"Oman", "Pakistan", "Panama", "Paraguay", "Peru", "Philippines", 
"Poland", "Portugal", "Qatar", "Republic of Korea", "Republic of Moldova", 
"Romania", "Russian Federation", "Rwanda", "Saint Lucia", "Sao Tome and Principe", 
"Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", 
"Singapore", "Slovakia", "Slovenia", "South Africa", "Spain", 
"Sri Lanka", "St. Vincent and the Grenadines", "State of Palestine", 
"Sudan", "Suriname", "Sweden", "Switzerland", "Syrian Arab Republic", 
"Taiwan", "Tajikistan", "Thailand", "Togo", "Trinidad and Tobago", 
"Tunisia", "Turkey", "Turkmenistan", "U.R. of Tanzania: Mainland", 
"Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", 
"United States of America", "Uruguay", "Uzbekistan", "Venezuela (Bolivarian Republic of)", 
"Viet Nam", "Yemen", "Zambia", "Zimbabwe"), outcomes = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("change_log_GDP_per_cap_1965_1990", 
"change_log_GDP_per_cap_1990_2015"), class = "factor"), value = c(NA, 
0.578640766531612, NA, NA, 0.495306867140096, NA, NA, 0.502330777653556, 
0.803114233961656, NA, NA, NA, -0.0993172653981915, 0.53808861725514, 
NA, 0.674597690030525, NA, -0.298127667954598, NA, 0.216504524313533, 
NA, 2.56081101010179, 0.744222824189625, NA, NA, 0.258214133266048, 
0.317611686261003, 0.8788161166173, NA, 0.421490462104297, 0.62364065369507, 
-0.232836544661687, -0.25227764221683, 0.231558185654343, 0.706630519833809, 
1.40191885822497, NA, 0.6181432134753, 0.408454749819774, 0.599584651840675, 
0.378345211978761, 0.111507643407153, NA, NA, 1.33884428004283, 
NA, -0.586751232211046, 0.474943966789859, NA, 0.627304433189133, 
0.317205519888644, 0.87991776954798, 0.0520672035501422, 0.285152006511415, 
NA, NA, 0.201711542768606, 0.769230971131345, 0.78251833772058, 
0.650001049678337, 0.640096625729845, -0.0303826601505452, NA, 
0.733078935225739, -0.389445841048811, 0.811272158246615, NA, 
0.347761117146236, 0.0460198222610888, 0.252620248084438, 0.0908892376466257, 
0.194886362609917, NA, 0.6418754594287, 0.129860579793292, 1.24788076169943, 
0.0596726474866065, NA, 0.846327343910559, 0.692715318495344, 
1.04082900921705, -0.178497178067447, 1.19255811694978, -0.105838641466363, 
NA, 0.109606385549342, NA, NA, NA, NA, NA, 0.303236240856323, 
-0.949968922781927, NA, 0.767732347388234, -0.161463624976531, 
0.020231695956479, 1.01233514958369, NA, 0.273235768821569, 1.7586551566631, 
-0.00512803730690603, 0.707921737818435, 0.467617173114609, NA, 
NA, 0.634355958220369, -0.0789716727055412, 0.0814288107000012, 
-0.243613157682208, 0.31709721264631, 0.610652584945905, 0.256647954302224, 
-0.781050134405546, -0.653436784876962, -1.60341160431009, NA, 
0.637654669091246, NA, 0.541902824704716, 0.513807926378716, 
0.731034519303526, 0.0790048395384364, 0.547280287550333, NA, 
0.980396213589966, NA, 2.20962119039451, NA, 1.24070927417225, 
NA, 0.292149277989863, NA, NA, NA, -0.0146454022990596, NA, 0.973529653896689, 
0.234827951040259, 1.84165010186193, NA, NA, 0.231253973970862, 
0.756869751851962, 0.0928048611587418, NA, NA, NA, NA, 0.511571399256075, 
0.469489935700883, -0.788333302649573, 1.76276690269169, NA, 
1.29149886770248, 0.109795534430917, 0.332264957334248, 1.06833555366236, 
0.683041196894621, NA, -0.305200676581399, -0.0697266221845751, 
NA, NA, 0.53776863243352, 0.548279184993683, 0.252697187479718, 
NA, 0.108634530140673, NA, NA, -0.809370440030817, 0.763010604986301, 
1.16118080105539, 0.345268208529435, 0.674459564759276, 0.375884558385717, 
1.12626530940127, 0.397946473384152, -0.00253749454397934, 0.533919266428686, 
0.670508443689885, 0.729139168827412, 0.138553807251364, 0.964924076948211, 
0.831001408021516, -0.107867285904469, 0.226109406112348, 0.565002710947052, 
0.284613448691427, 0.49671777747743, 1.08871795603012, 0.965320933057071, 
2.0824942302721, 0.909321382997916, 0.90572923716188, 0.0745240787206782, 
0.56342165427044, 0.518270859785026, -0.14261584736499, 1.11596521067868, 
1.07746977455876, 0.234995617759376, 0.351972663095612, -0.443934223486251, 
0.400139141337132, 1.11273343990489, 1.55236005828284, 0.801714457394146, 
1.36083105766367, 0.605176130472083, -0.214126332742843, 0.525398196260118, 
0.727131320016225, 0.363476755488312, 0.592511243723756, NA, 
0.393009253653243, 0.395363456453468, -0.500484422514334, 0.576601147212303, 
0.212354523986997, 1.10506821483667, 0.58045978480669, 1.60319871386148, 
1.00648719095641, 2.34871552055859, 0.989017399345231, 0.296900628912704, 
0.69635119246042, 0.397167906662684, 0.476036716075047, 0.455084710211471, 
0.387214183430407, -0.0991552203097266, -0.0267406304735509, 
0.584919113694179, 0.578109146112334, 0.42027754849506, 0.959093836130545, 
0.702232988685832, -0.489186633293355, 0.0667617953291728, 0.272108047914668, 
0.430569823721677, 0.723918391584105, 0.418479258591489, 1.46692343017624, 
1.13810360000623, 1.58208109756767, 1.14483586081786, 1.34109776560516, 
0.547783112654397, 0.378827744259436, 0.443336132108021, 0.390603398945073, 
0.979563275303992, 0.677796146444582, 0.30945873654342, 0.811130009728783, 
-0.698518017728585, 1.5965365998623, 0.428944840390766, 1.35340343539671, 
0.72302713452681, 0.0165685053242486, 0.73992426940646, 0.834336855944846, 
0.169723309384593, 0.124024117537306, 1.09861308721801, 1.19942465415223, 
1.19380578812553, 0.978983580563993, 0.438319759789805, 0.724114030801147, 
0.450473671944929, 1.23922185191996, 0.394164965021167, 0.657274225873365, 
0.855678052044124, 1.85192687683034, 0.86999914293389, 0.827673817687534, 
0.601326117535573, 0.56384253841845, 0.502420140731956, 0.0558758572526301, 
1.77296464408553, 0.825623246808037, 0.795992808711354, 1.12006525936429, 
0.636770955714099, 1.20754087091403, 0.791845392249831, 1.1755507928123, 
0.681217042180291, 1.17322395658813, 0.675244390301616, 1.36348537713933, 
1.07417558474477, -0.068430313534666, 1.08019795067758, 0.47038618934369, 
0.521107429716069, 0.482651259569048, 0.573592386846872, 0.922428279005258, 
0.165721243515427, 0.385694688986387, 0.551362682375665, -0.117354750364616, 
1.3168837696292, 0.522290759400617, 0.476744898537484, 0.377802643502404, 
0.684679934216977, 1.36583491854445, 0.580631004730677, 0.539972392301982, 
1.03119161251096, 0.82686270229947, 0.522366896147473, 0.481811274117581, 
1.25317892492354, 0.884653998710446, -0.749377267033438, 1.13515779802437, 
-0.112487015998199, 1.41812368189913, 0.700084748207621, 0.891842804137902, 
0.702757564431742, 0.734076268566994, 0.816010460696261, -0.184716040330949, 
-0.465181926681245, 0.568585655970242, 0.411752804691037, 0.810583157313205, 
0.554500820737744, -0.145349395370232, 1.49133874195868, 1.23056235160133, 
1.02549436830091, -0.839955549686024)), row.names = c(NA, -346L
), class = "data.frame")

structure(list(Country = c("Albania", "Algeria", "Angola", "Antigua and Barbuda", 
"Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", 
"Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", 
"Belize", "Benin", "Bhutan", "Bolivia (Plurinational State of)", 
"Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei Darussalam", 
"Bulgaria", "Burkina Faso", "Burundi", "Cabo Verde", "Cambodia", 
"Cameroon", "Canada", "Central African Republic", "Chad", "Chile", 
"China", "China, Hong Kong SAR", "China, Macao SAR", "Colombia", 
"Comoros", "Congo", "Costa Rica", "Côte d'Ivoire", "Croatia", 
"Curaçao", "Cyprus", "Czechia", "D.R. of the Congo", "Denmark", 
"Djibouti", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", 
"Equatorial Guinea", "Estonia", "Eswatini", "Ethiopia", "Fiji", 
"Finland", "France", "Gabon", "Gambia", "Georgia", "Germany", 
"Ghana", "Greece", "Grenada", "Guatemala", "Guinea", "Guinea-Bissau", 
"Haiti", "Honduras", "Hungary", "Iceland", "India", "Indonesia", 
"Iran (Islamic Republic of)", "Iraq", "Ireland", "Israel", "Italy", 
"Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kuwait", 
"Kyrgyzstan", "Lao People's DR", "Latvia", "Lebanon", "Lesotho", 
"Liberia", "Lithuania", "Luxembourg", "Madagascar", "Malawi", 
"Malaysia", "Maldives", "Mali", "Malta", "Mauritania", "Mauritius", 
"Mexico", "Mongolia", "Montenegro", "Morocco", "Mozambique", 
"Myanmar", "Namibia", "Nepal", "Netherlands", "New Zealand", 
"Nicaragua", "Niger", "Nigeria", "North Macedonia", "Norway", 
"Oman", "Pakistan", "Panama", "Paraguay", "Peru", "Philippines", 
"Poland", "Portugal", "Qatar", "Republic of Korea", "Republic of Moldova", 
"Romania", "Russian Federation", "Rwanda", "Saint Lucia", "Sao Tome and Principe", 
"Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", 
"Singapore", "Slovakia", "Slovenia", "South Africa", "Spain", 
"Sri Lanka", "St. Vincent and the Grenadines", "State of Palestine", 
"Sudan", "Suriname", "Sweden", "Switzerland", "Syrian Arab Republic", 
"Taiwan", "Tajikistan", "Thailand", "Togo", "Trinidad and Tobago", 
"Tunisia", "Turkey", "Turkmenistan", "U.R. of Tanzania: Mainland", 
"Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", 
"United States of America", "Uruguay", "Uzbekistan", "Venezuela (Bolivarian Republic of)", 
"Viet Nam", "Yemen", "Zambia", "Zimbabwe", "Albania", "Algeria", 
"Angola", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", 
"Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", 
"Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bhutan", 
"Bolivia (Plurinational State of)", "Bosnia and Herzegovina", 
"Botswana", "Brazil", "Brunei Darussalam", "Bulgaria", "Burkina Faso", 
"Burundi", "Cabo Verde", "Cambodia", "Cameroon", "Canada", "Central African Republic", 
"Chad", "Chile", "China", "China, Hong Kong SAR", "China, Macao SAR", 
"Colombia", "Comoros", "Congo", "Costa Rica", "Côte d'Ivoire", 
"Croatia", "Curaçao", "Cyprus", "Czechia", "D.R. of the Congo", 
"Denmark", "Djibouti", "Dominican Republic", "Ecuador", "Egypt", 
"El Salvador", "Equatorial Guinea", "Estonia", "Eswatini", "Ethiopia", 
"Fiji", "Finland", "France", "Gabon", "Gambia", "Georgia", "Germany", 
"Ghana", "Greece", "Grenada", "Guatemala", "Guinea", "Guinea-Bissau", 
"Haiti", "Honduras", "Hungary", "Iceland", "India", "Indonesia", 
"Iran (Islamic Republic of)", "Iraq", "Ireland", "Israel", "Italy", 
"Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kuwait", 
"Kyrgyzstan", "Lao People's DR", "Latvia", "Lebanon", "Lesotho", 
"Liberia", "Lithuania", "Luxembourg", "Madagascar", "Malawi", 
"Malaysia", "Maldives", "Mali", "Malta", "Mauritania", "Mauritius", 
"Mexico", "Mongolia", "Montenegro", "Morocco", "Mozambique", 
"Myanmar", "Namibia", "Nepal", "Netherlands", "New Zealand", 
"Nicaragua", "Niger", "Nigeria", "North Macedonia", "Norway", 
"Oman", "Pakistan", "Panama", "Paraguay", "Peru", "Philippines", 
"Poland", "Portugal", "Qatar", "Republic of Korea", "Republic of Moldova", 
"Romania", "Russian Federation", "Rwanda", "Saint Lucia", "Sao Tome and Principe", 
"Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", 
"Singapore", "Slovakia", "Slovenia", "South Africa", "Spain", 
"Sri Lanka", "St. Vincent and the Grenadines", "State of Palestine", 
"Sudan", "Suriname", "Sweden", "Switzerland", "Syrian Arab Republic", 
"Taiwan", "Tajikistan", "Thailand", "Togo", "Trinidad and Tobago", 
"Tunisia", "Turkey", "Turkmenistan", "U.R. of Tanzania: Mainland", 
"Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", 
"United States of America", "Uruguay", "Uzbekistan", "Venezuela (Bolivarian Republic of)", 
"Viet Nam", "Yemen", "Zambia", "Zimbabwe"), outcomes = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("change_dependency_ratio_1990_2015", 
"change_dependency_ratio_1965_1990"), class = "factor"), value = c(0.398123161188132, 
0.0830910838830155, -0.0509010462858756, 0.14130939757635, 0.0309448471342206, 
0.212156096018974, 0.467114123861573, 0.242160484950692, 0.253948731466323, 
0.0388882219981139, 0.184086975367716, 0.0554969720621382, 0.024214947271092, 
0.403122685405711, 0.122196022985809, 0.221554441449814, 0.0105808383432469, 
-0.0350666654672087, 0.0237230699085401, 0.0542006723001102, 
0.396301148636019, 0.00479665619727526, 0.17613557364792, 0.158549300687729, 
0.222753904572088, -0.0679851322620935, -0.0253231953457055, 
-0.0867506561598986, 0.0406618446863762, -0.083315964393983, 
0.393556442177755, -0.0775416079610512, -0.0658887855682476, 
0.231645867769874, 0.229567396079183, 0.433009260575827, 0.294212666921972, 
0.188762457136509, -0.0265371130376149, -0.0263212095409177, 
0.214474587251249, -0.0139063070849487, 0.322621916655375, 0.587328280156014, 
0.120008191472176, 0.213159693532978, -0.00987639022713166, 0.290196653308586, 
0.045992636949827, 0.119538595241012, 0.120165701587676, -0.00708999590830717, 
0.104787443391876, -0.165762118505394, 0.241094479011241, -0.0040719308805422, 
-0.0267978243769081, 0.168850352663662, 0.44319872930422, 0.323540395295048, 
-0.23131326653667, -0.00875776820505886, 0.164852036810846, 0.374007815296586, 
0.00886861846944575, 0.206341509473451, 0.0545333305953083, -0.0212696351809918, 
-0.0498839597353738, -0.039324667932303, -0.0134550789380752, 
0.0146799292807489, 0.15773040039105, 0.257911755378933, 0.0770514810343421, 
0.0782733233080137, 0.0276023447303095, -0.077064515546113, 0.135642580624321, 
0.144829445263811, 0.319736193367588, 0.0904353552944108, 0.514329009047002, 
-0.0148943228589375, 0.0423996039440057, -0.0328708559176694, 
0.0858069299988889, -0.0532225891248916, -0.0227918426094847, 
0.261177457981367, 0.0172789840634143, -0.044950556774743, -0.0381052703009499, 
0.339264196021887, 0.0678084085627291, -0.018091746025337, -0.0454328686516391, 
0.110564793514707, -0.123460223196024, -0.107107285546687, 0.394036685146817, 
-0.0219956010018594, 0.358122825326543, 0.116941994109051, -0.0159830755685619, 
0.2769238725963, 0.113357348751862, -0.0995079697291118, 0.0830925437136069, 
-0.0231308148595351, 0.0818960835398407, 0.406916883761686, 0.283544729400408, 
0.0473232423265355, 0.0256750433301466, -0.0322884763794392, 
0.226824565750246, 0.143678691632955, -0.0525910860693484, -0.0192164663568681, 
0.145296134674028, 0.0777810924257498, 0.138812789996608, 0.108991312209926, 
0.255085975411616, 0.288899001012185, -0.0056058738464385, 0.411675880202299, 
0.0979338974291016, 0.158261393920507, 0.121463375174013, 0.03221558529891, 
0.0752098777829092, -0.108038719584569, 0.0451082001661763, -0.0488696890082719, 
0.264057297418888, 0.0744761732040302, -0.0808960422374715, 0.377536265597609, 
0.203639151129089, 0.360038671966065, 0.016648408952926, 0.187105929024774, 
0.256758186659192, 0.199214226926007, 0.0141836912145429, 0.0238674633141847, 
0.107634547878027, 0.158412785555059, 0.227923174333828, 0.0222261028045417, 
0.356964682522796, -0.0688202726456453, 0.334653694535773, -0.0360247564281497, 
0.251095568507597, 0.111701591703071, 0.107279630093633, 0.0181998814698253, 
-0.0227988569105966, -0.0504624773401749, 0.0704011548529228, 
0.00834067678623526, 0.17363275607521, 0.29244025856914, 0.0578621209808398, 
0.00152730146499902, 0.149545103984778, 0.0647895136705983, -0.0305648272827664, 
-0.0506354698086458, NA, -0.0292982602120182, -0.0745568714193291, 
-0.0158114995499791, -0.0526278674865631, 0.0895692260747208, 
0.0622753451174147, 0.0693631196029568, -0.043524303709262, -0.163336152067602, 
0.0220069231850007, -0.0372480350396547, -0.0127416788704636, 
0.00504322809867846, -0.211303990526285, 0.150689604961094, -0.0774764273136075, 
-0.107327548073341, -0.101653276820036, 0.0541427833880424, 0.0053919747731232, 
0.158611219787252, -0.0491640268190917, 0.00338102629153109, 
-0.155711219022126, 0.199503979128721, 0.000143811528831528, 
-0.0505998291341132, -0.0683908203869206, 0.00531631981392033, 
-0.0188264582510709, -0.00972775658891123, -0.0423089269872168, 
-0.0405244974334903, 0.0600854559092546, -0.00915296736548726, 
0.115110360580881, -0.171529225844077, -0.00248223661953856, 
-0.0232805426144127, -0.0525986053355909, 0.00558486661777702, 
0.0144074940917683, 0.14710582191989, 0.0316717285501824, -0.00945326566098326, 
-0.13736299087858, 0.0132915916588611, -0.050472174498465, 0.00652518868343993, 
0.0377821706637551, -0.0700536501269396, 0.00242593772672384, 
0.0509922622906901, -0.0590559970975604, 0.0550947644834419, 
-0.044565454128298, 0.0307192650398631, 0.018881004597837, 0.0235594502081332, 
-0.100079012305613, -0.0366233215469571, 0.00298230971394092, 
0.158094632962475, -0.0696033527255899, 0.00675969760095929, 
0.242793615992266, 0.0289955515148832, 0.0520204945268078, 0.00911963582072006, 
-0.069540427821399, 0.000208817704798414, -0.0342934097114747, 
0.0226814556538434, -0.0463245292019691, 0.00722298596780935, 
0.00718862780849888, -0.00066619912582111, -0.0767116018378476, 
-0.243121449034015, -0.0443807611718632, 0.0932646168511789, 
-0.0671773042384913, 0.284002072219196, -0.00547861010293765, 
0.0343447054170564, -0.111773356351767, 0.00322001522478446, 
-0.0587805770660498, 0.016093164362485, 0.0686994785554735, -0.0341956195178609, 
-0.0622551944564226, 0.0302916484213724, 0.0878515589816369, 
-0.0958075512267668, -0.0718260301242502, 0.00658730728919477, 
-0.0407145080805323, 0.126027524598038, 0.0353731549521963, -0.04687770096068, 
0.0194541107438475, 0.0472818974219094, -0.024043289427089, -0.120483950102178, 
0.161455102896429, 0.0435832542273855, 0.039171762761035, -0.0183988230174034, 
-0.044959894325082, 0.0246249760978033, -0.0464051312493554, 
-0.0398124625969878, 0.0232230074702894, 0.0418812430599326, 
0.0121811028455855, 0.112629852328967, -0.0953347596912298, -0.121894030853175, 
0.0175797098796357, -0.0048370366236079, -0.0305903547470887, 
-0.0183306921407706, -0.0641072715448618, 0.0318404615702211, 
0.149207705011576, -0.0596233194534432, 0.0187959558456414, 0.0918171139063938, 
0.133846112123352, 0.184054472200999, -0.0565179188347451, 0.0141032047121729, 
0.1361470326673, -0.128088011242165, 0.0315299754663041, 0.150424334795938, 
-0.0714527750333729, -0.00445779662263851, -0.0277364410930278, 
-0.0463879783356869, 0.00475683969862661, -0.0194317397134581, 
0.136516248539494, 0.0284509475772005, -0.110705732900706, -0.0517643559854549, 
-0.0216934415592076, -0.00598072520888693, -0.0201970082427561, 
0.00420968059751337, -0.0588788836438903, 0.0964065165788101, 
-0.0606627161874218, 0.0290743755043736, -0.00979923240980679, 
-0.00601622584197931, 0.0296275948272899, 0.015878161506364, 
-0.107568232847218, -0.000340675297749504, -0.0184271677103232, 
0.214065248760853, -0.0565794859933912, -0.0762034351837085, 
-0.0872304067027536, 0.154039412247359, -0.133206610906594, 0.0246356772631723, 
-0.0883994807900984, -0.0526459846319143, -0.021995026737945, 
-0.0624685869818875)), row.names = c(NA, -346L), class = "data.frame")

这是我的代码:

    df_ratio_stacked <- melt(df_ratio_stacked, id.var = c('Country'), variable.name = 'outcomes')
  df_gdp_stacked <- melt(df_gdp_stacked, id.var = c('Country'), variable.name = 'outcomes')
  #Merging
  df_stacked = merge(x=df_ratio_stacked,y=df_gdp_stacked, by='Country')

当我合并两个数据集时,我得到了国家和结果的副本,因此将有 4 行而不是所需的 2 行。

我尝试使用 distinct 函数,它有效但仅适用于两者之一。我的意思是,当我使用 distinct 时,我无法摆脱 outcome.x 和 outcome.y

的重复项

我希望我的问题有点清楚,并且有人能够提供帮助!

外观示例:

而不是 merge 我认为你应该 rbind 这两个数据集。为清楚起见,您可以获取宽格式的数据,这样每个国家/地区只有 1 行。

library(dplyr)
library(tidyr)

bind_rows(df1, df2) %>%
  mutate(outcomes = sub('_\d+_\d+', '', outcomes)) %>%
  pivot_wider(names_from = outcomes, values_from = value, 
              values_fn = function(x) mean(x, na.rm = TRUE))

# A tibble: 173 x 3
#    Country             change_log_GDP_per_cap change_dependency_ratio
#   <chr>                                <dbl>                   <dbl>
# 1 Albania                            1.16                    0.184  
# 2 Algeria                            0.462                   0.00427
# 3 Angola                             0.674                  -0.0334 
# 4 Antigua and Barbuda                0.376                   0.0443 
# 5 Argentina                          0.811                   0.0603 
# 6 Armenia                            0.398                   0.137  
# 7 Aruba                             -0.00254                 0.268  
# 8 Australia                          0.518                   0.0993 
# 9 Austria                            0.737                   0.0453 
#10 Azerbaijan                         0.729                   0.0304 
# … with 163 more rows

表示数据的另一种方式是使用嵌套 -

df1 %>%
  nest(data1 = -Country) %>%
  inner_join(df2 %>%
  nest(data2 = -Country), by = 'Country') %>%
  unnest_wider(col = data1, names_sep = '_') %>%
  unnest_wider(col = data2, names_sep = '_') %>%
  unnest(cols = -Country)

# A tibble: 346 x 5
#   Country             data1_outcomes                   data1_value data2_outcomes                    data2_value
#   <chr>               <fct>                                  <dbl> <fct>                                   <dbl>
# 1 Albania             change_log_GDP_per_cap_1965_1990      NA     change_dependency_ratio_1990_2015      0.398 
# 2 Albania             change_log_GDP_per_cap_1990_2015       1.16  change_dependency_ratio_1965_1990     -0.0293
# 3 Algeria             change_log_GDP_per_cap_1965_1990       0.579 change_dependency_ratio_1990_2015      0.0831
# 4 Algeria             change_log_GDP_per_cap_1990_2015       0.345 change_dependency_ratio_1965_1990     -0.0746
# 5 Angola              change_log_GDP_per_cap_1965_1990      NA     change_dependency_ratio_1990_2015     -0.0509
# 6 Angola              change_log_GDP_per_cap_1990_2015       0.674 change_dependency_ratio_1965_1990     -0.0158
# 7 Antigua and Barbuda change_log_GDP_per_cap_1965_1990      NA     change_dependency_ratio_1990_2015      0.141 
# 8 Antigua and Barbuda change_log_GDP_per_cap_1990_2015       0.376 change_dependency_ratio_1965_1990     -0.0526
# 9 Argentina           change_log_GDP_per_cap_1965_1990       0.495 change_dependency_ratio_1990_2015      0.0309
#10 Argentina           change_log_GDP_per_cap_1990_2015       1.13  change_dependency_ratio_1965_1990      0.0896
# … with 336 more rows