These are the first 50 rows of my dataset:
structure(list(price_used = c("comtrade_price_mult", NA, NA,
"comtrade_price_mult", "comtrade_price_mult", "comtrade_price_mult",
"comtrade_price_mult", "comtrade_price_mult", "comtrade_price_mult",
"comtrade_price_mult", "comtrade_price_mult", "comtrade_price_mult",
"comtrade_price_mult", "comtrade_price_mult", "comtrade_price_mult",
"comtrade_price_mult", "comtrade_price_mult", "comtrade_price_mult",
"comtrade_price_mult", "comtrade_price_mult", "comtrade_price_mult",
"comtrade_price_mult", "comtrade_price_mult", "comtrade_price_mult",
"comtrade_price_mult", "comtrade_price_mult", "comtrade_price_mult",
"comtrade_price_mult", "comtrade_price_mult", "comtrade_price_mult",
NA, "comtrade_price_mult", "comtrade_price_mult", NA, "comtrade_price_mult",
NA, "comtrade_price_mult", "comtrade_price_mult", "comtrade_price_mult",
NA, "comtrade_price_mult", "comtrade_price_mult", "comtrade_price_mult",
NA, "comtrade_price_mult", NA, "comtrade_price_mult", "comtrade_price_mult",
"comtrade_price_mult", NA), year = c(2003, 2013, 2014, 2005,
2005, 2005, 2005, 2006, 2006, 2006, 2006, 2007, 2007, 2007, 2007,
2008, 2008, 2009, 2009, 2010, 2010, 2011, 2011, 2012, 2012, 2012,
2012, 1994, 1995, 1996, 1996, 1996, 1997, 1997, 1997, 1998, 1998,
1998, 1999, 1999, 1999, 2004, 2004, 2004, 2005, 2005, 2005, 2006,
2006, 2006), country = c("cameroon", "cape verde", "cape verde",
"cote d'ivoire", "cote d'ivoire", "cote d'ivoire", "cote d'ivoire",
"cote d'ivoire", "cote d'ivoire", "cote d'ivoire", "cote d'ivoire",
"cote d'ivoire", "cote d'ivoire", "cote d'ivoire", "cote d'ivoire",
"cote d'ivoire", "cote d'ivoire", "cote d'ivoire", "cote d'ivoire",
"cote d'ivoire", "cote d'ivoire", "cote d'ivoire", "cote d'ivoire",
"cote d'ivoire", "cote d'ivoire", "cote d'ivoire", "cote d'ivoire",
"egypt", "egypt", "egypt", "egypt", "egypt", "egypt", "egypt",
"egypt", "egypt", "egypt", "egypt", "egypt", "egypt", "egypt",
"egypt", "egypt", "egypt", "egypt", "egypt", "egypt", "egypt",
"egypt", "egypt"), standardmeasure = c("42-gallon barrels per day",
"", "", "thousand 42-gallon barrels", "thousand 42-gallon barrels",
"thousand 42-gallon barrels", "thousand 42-gallon barrels", "thousand 42-gallon barrels",
"thousand 42-gallon barrels", "thousand 42-gallon barrels", "thousand 42-gallon barrels",
"thousand 42-gallon barrels", "thousand 42-gallon barrels", "thousand 42-gallon barrels",
"thousand 42-gallon barrels", "thousand 42-gallon barrels", "thousand 42-gallon barrels",
"thousand 42-gallon barrels", "thousand 42-gallon barrels", "thousand 42-gallon barrels",
"thousand 42-gallon barrels", "thousand 42-gallon barrels", "thousand 42-gallon barrels",
"thousand 42-gallon barrels", "thousand 42-gallon barrels", "thousand 42-gallon barrels",
"thousand 42-gallon barrels", "million 42-gallon barrels", "million 42-gallon barrels",
"million 42-gallon barrels", "million cubic meters", "million 42-gallon barrels",
"million 42-gallon barrels", "million cubic meters", "million 42-gallon barrels",
"million cubic meters", "million 42-gallon barrels", "million 42-gallon barrels",
"million 42-gallon barrels", "million cubic meters", "million 42-gallon barrels",
"million 42-gallon barrels", "million 42-gallon barrels", "million cubic meters",
"million 42-gallon barrels", "million cubic meters", "million 42-gallon barrels",
"million 42-gallon barrels", "million 42-gallon barrels", "million cubic meters"
), annuallocationcapacity = c(5250, NA, NA, 1000, 1000, 17520,
6700, 1000, 17520, 6700, 1000, 1000, 6700, 17520, 1000, 17520,
6700, 6700, 17520, 6700, 17520, 6700, 17520, 6700, NA, NA, 17520,
42, 42, 15, 3800, 27, 20, 5000, 12, 3800, 15, 27, 27, 3800, 15,
27, 15, 3800, 15, 3800, 27, 27, 15, 3800), minetype = c("extraction site",
"mine", "mine", "extraction site", "extraction site", "extraction site",
"extraction site", "extraction site", "extraction site", "extraction site",
"extraction site", "extraction site", "extraction site", "extraction site",
"extraction site", "extraction site", "extraction site", "extraction site",
"extraction site", "extraction site", "extraction site", "extraction site",
"extraction site", "extraction site", "extraction site", "extraction site",
"extraction site", "extraction site", "extraction site", "extraction site",
"extraction site", "extraction site", "extraction site", "extraction site",
"extraction site", "extraction site", "extraction site", "extraction site",
"extraction site", "extraction site", "extraction site", "extraction site",
"extraction site", "extraction site", "extraction site", "extraction site",
"extraction site", "extraction site", "extraction site", "extraction site"
), mineownership = c("industrial", "artisanal", "artisanal",
"industrial", "industrial", "industrial", "industrial", "industrial",
"industrial", "industrial", "industrial", "industrial", "industrial",
"industrial", "industrial", "industrial", "industrial", "industrial",
"industrial", "industrial", "industrial", "industrial", "industrial",
"industrial", "industrial", "industrial", "industrial", "government",
"government", "government", "government", "government", "government",
"government", "government", "government", "government", "government",
"industrial", "government", "industrial", "government", "government",
"government", "government", "government", "government", "government",
"government", "government"), locationname = c("moudi field",
"salinas de porto ingles, maio", "salinas de porto ingles, maio",
"panther oilfield", "lion oilfield", "baobab oilfield", "east espoir oilfield",
"panther oilfield", "baobab oilfield", "east espoir oilfield",
"lion oilfield", "lion oilfield", "east espoir oilfield", "baobab oilfield",
"panther oilfield", "baobab oilfield", "east espoir oilfield",
"east espoir oilfield", "baobab oilfield", "east espoir oilfield",
"baobab oilfield", "east espoir oilfield", "baobab oilfield",
"east espoir oilfield", "block cl-401", "block cl-103", "baobab oilfield",
"el morgan, suez gulf", "el morgan, suez gulf", "ras budran, suez gulf",
"abu madi", "el morgan, suez gulf", "el morgan, suez gulf", "abu madi",
"ras budran, suez gulf", "abu madi", "ras budran, suez gulf",
"el morgan, suez gulf", "el morgan, suez gulf", "abu madi", "ras budran, suez gulf",
"el morgan, suez gulf", "ras budran, suez gulf", "abu madi",
"ras budran, suez gulf", "abu madi", "el morgan, suez gulf",
"el morgan, suez gulf", "ras budran, suez gulf", "abu madi"),
admin1 = c("", "maio", "maio", "gulf of guinea", "gulf of guinea",
"gulf of guinea", "gulf of guinea", "gulf of guinea", "gulf of guinea",
"gulf of guinea", "gulf of guinea", "gulf of guinea", "gulf of guinea",
"gulf of guinea", "gulf of guinea", "gulf of guinea", "gulf of guinea",
"gulf of guinea", "gulf of guinea", "gulf of guinea", "gulf of guinea",
"gulf of guinea", "gulf of guinea", "gulf of guinea", "gulf of guinea",
"gulf of guinea", "gulf of guinea", "south sinai", "south sinai",
"south sinai", "matruh", "south sinai", "south sinai", "matruh",
"south sinai", "matruh", "south sinai", "south sinai", "south sinai",
"matruh", "south sinai", "south sinai", "south sinai", "matruh",
"south sinai", "matruh", "south sinai", "south sinai", "south sinai",
"matruh"), admin2 = c("", "porto ingles", "porto ingles",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", ""), world_val_withmc = c(64626612, NA, NA, 58750496,
58750496, 1029308672, 393628320, 68633024, 1202450560, 459841248,
68633024, 73954616, 495495904, 1295684864, 73954616, 1733089280,
662768192, 418824960, 1095196032, 529573184, 1384794368,
682766400, 1785383168, 676864192, NA, NA, 1769949312, 915343488,
969826176, 404228960, NA, 727612096, 497491968, NA, 298495168,
NA, 251538608, 452769472, 616853568, NA, 342696416, 1157142272,
642856832, NA, 881257408, NA, 1586263424, 1853091584, 1029495296,
NA), lootable = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), orig_deflator = c(NA,
105.639106949701, 107.374395983726, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), resource = c("oil",
"salt", "salt", "oil", "oil", "oil", "oil", "oil", "oil",
"oil", "oil", "oil", "oil", "oil", "oil", "oil", "oil", "oil",
"oil", "oil", "oil", "oil", "oil", "oil", "oil", "oil", "oil",
"oil", "oil", "oil", "natural gas", "oil", "oil", "natural gas",
"oil", "natural gas", "oil", "oil", "oil", "natural gas",
"oil", "oil", "oil", "natural gas", "oil", "natural gas",
"oil", "oil", "oil", "natural gas"), exp_annual_value_location1 = c(76133440,
NA, NA, 62305808, 62305808, 1091597824, 417448928, 66877492,
1171693696, 448079200, 66877492, 76446016, 512188320, 1339334144,
76446016, 1643753728, 628604480, 418313280, 1093857920, 529067712,
1383472512, 717797248, 1876986112, 748939840, NA, NA, 1958421888,
877859392, 847055424, 365471232, NA, 657848192, 426205312,
NA, 255723200, NA, 168767872, 303782144, 536430400, NA, 298016896,
912430656, 506905920, NA, 593088704, NA, 1067559680, 1312398336,
729110144, NA), offshore = c(1, 0, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1), NAME_0 = c("Cameroon", "Cabo Verde", "Cabo Verde",
"Côte d'Ivoire", "Côte d'Ivoire", "Côte d'Ivoire", "Côte d'Ivoire",
"Côte d'Ivoire", "Côte d'Ivoire", "Côte d'Ivoire", "Côte d'Ivoire",
"Côte d'Ivoire", "Côte d'Ivoire", "Côte d'Ivoire", "Côte d'Ivoire",
"Côte d'Ivoire", "Côte d'Ivoire", "Côte d'Ivoire", "Côte d'Ivoire",
"Côte d'Ivoire", "Côte d'Ivoire", "Côte d'Ivoire", "Côte d'Ivoire",
"Côte d'Ivoire", "Côte d'Ivoire", "Côte d'Ivoire", "Côte d'Ivoire",
"Egypt", "Egypt", "Egypt", "Egypt", "Egypt", "Egypt", "Egypt",
"Egypt", "Egypt", "Egypt", "Egypt", "Egypt", "Egypt", "Egypt",
"Egypt", "Egypt", "Egypt", "Egypt", "Egypt", "Egypt", "Egypt",
"Egypt", "Egypt"), NAME_1 = c("Sud-Ouest", "Maio", "Maio",
"Lagunes", "Lagunes", "Lagunes", "Lagunes", "Lagunes", "Lagunes",
"Lagunes", "Lagunes", "Lagunes", "Lagunes", "Lagunes", "Lagunes",
"Lagunes", "Lagunes", "Lagunes", "Lagunes", "Lagunes", "Lagunes",
"Lagunes", "Lagunes", "Lagunes", "Comoe", "Abidjan", "Lagunes",
"Janub Sina", "Janub Sina", "Janub Sina", "Al Iskandariyah",
"Janub Sina", "Janub Sina", "Al Iskandariyah", "Janub Sina",
"Al Iskandariyah", "Janub Sina", "Janub Sina", "Janub Sina",
"Al Iskandariyah", "Janub Sina", "Janub Sina", "Janub Sina",
"Al Iskandariyah", "Janub Sina", "Al Iskandariyah", "Janub Sina",
"Janub Sina", "Janub Sina", "Al Iskandariyah"), comtrade_price_mult = c(14501.607421875,
NA, NA, 62305.80859375, 62305.80859375, 62305.80859375, 62305.80859375,
66877.4921875, 66877.4921875, 66877.4921875, 66877.4921875,
76446.015625, 76446.015625, 76446.015625, 76446.015625, 93821.5625,
93821.5625, 62434.81640625, 62434.81640625, 78965.328125,
78965.328125, 107133.9140625, 107133.9140625, 111782.0703125,
111782.0703125, 111782.0703125, 111782.0703125, 20901414,
20167986, 24364748, NA, 24364748, 21310266, NA, 21310266,
NA, 11251191, 11251191, 19867792, NA, 19867792, 33793728,
33793728, NA, 39539248, NA, 39539248, 48607344, 48607344,
NA), wb_price_mult = c(12309.830078125, NA, NA, 58750.49609375,
58750.49609375, 58750.49609375, 58750.49609375, 68633.0234375,
68633.0234375, 68633.0234375, 68633.0234375, 73954.6171875,
73954.6171875, 73954.6171875, 73954.6171875, 98920.625, 98920.625,
62511.19140625, 62511.19140625, 79040.7734375, 79040.7734375,
101905.4296875, 101905.4296875, 101024.5078125, 101024.5078125,
101024.5078125, 101024.5078125, 21793892, 23091100, 26948596,
NA, 26948596, 24874598, NA, 24874598, NA, 16769240, 16769240,
22846428, NA, 22846428, 42857120, 42857120, NA, 58750496,
NA, 58750496, 68633024, 68633024, NA), usgs_price_mult = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), multicolour_price_mult = c(NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), comtrade_unit = c("kilograms", "", "", "kilograms", "kilograms",
"kilograms", "kilograms", "kilograms", "kilograms", "kilograms",
"kilograms", "kilograms", "kilograms", "kilograms", "kilograms",
"kilograms", "kilograms", "kilograms", "kilograms", "kilograms",
"kilograms", "kilograms", "kilograms", "kilograms", "kilograms",
"kilograms", "kilograms", "kilograms", "kilograms", "kilograms",
"", "kilograms", "kilograms", "", "kilograms", "", "kilograms",
"kilograms", "kilograms", "", "kilograms", "kilograms", "kilograms",
"", "kilograms", "", "kilograms", "kilograms", "kilograms",
""), wb_unit = c("42-gallon barrels", "", "", "42-gallon barrels",
"42-gallon barrels", "42-gallon barrels", "42-gallon barrels",
"42-gallon barrels", "42-gallon barrels", "42-gallon barrels",
"42-gallon barrels", "42-gallon barrels", "42-gallon barrels",
"42-gallon barrels", "42-gallon barrels", "42-gallon barrels",
"42-gallon barrels", "42-gallon barrels", "42-gallon barrels",
"42-gallon barrels", "42-gallon barrels", "42-gallon barrels",
"42-gallon barrels", "42-gallon barrels", "42-gallon barrels",
"42-gallon barrels", "42-gallon barrels", "42-gallon barrels",
"42-gallon barrels", "42-gallon barrels", "", "42-gallon barrels",
"42-gallon barrels", "", "42-gallon barrels", "", "42-gallon barrels",
"42-gallon barrels", "42-gallon barrels", "", "42-gallon barrels",
"42-gallon barrels", "42-gallon barrels", "", "42-gallon barrels",
"", "42-gallon barrels", "42-gallon barrels", "42-gallon barrels",
""), usgs_unit = c("", "metric tons", "metric tons", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
""), multicolour_unit = c("", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", ""), comtrade_price = c(0.288748025894165,
NA, NA, 0.461126893758774, 0.461126893758774, 0.461126893758774,
0.461126893758774, 0.494962066411972, 0.494962066411972,
0.494962066411972, 0.494962066411972, 0.565778911113739,
0.565778911113739, 0.565778911113739, 0.565778911113739,
0.694375813007355, 0.694375813007355, 0.462081670761108,
0.462081670761108, 0.584424436092377, 0.584424436092377,
0.792900860309601, 0.792900860309601, 0.827301979064941,
0.827301979064941, 0.827301979064941, 0.827301979064941,
0.150046527385712, 0.144781410694122, 0.174909025430679,
NA, 0.174909025430679, 0.152981579303741, NA, 0.152981579303741,
NA, 0.0807697549462318, 0.0807697549462318, 0.142626389861107,
NA, 0.142626389861107, 0.242597535252571, 0.242597535252571,
NA, 0.283843338489532, NA, 0.283843338489532, 0.348941147327423,
0.348941147327423, NA), wb_price = c(33.7255630927967, NA,
NA, 58.7504956668303, 58.7504956668303, 58.7504956668303,
58.7504956668303, 68.6330215319166, 68.6330215319166, 68.6330215319166,
68.6330215319166, 73.954614038387, 73.954614038387, 73.954614038387,
73.954614038387, 98.9206215110437, 98.9206215110437, 62.5111900128862,
62.5111900128862, 79.0407717847173, 79.0407717847173, 101.905430027248,
101.905430027248, 101.024504690732, 101.024504690732, 101.024504690732,
101.024504690732, 21.7938920399316, 23.0910996292376, 26.9485969327739,
NA, 26.9485969327739, 24.8745986032634, NA, 24.8745986032634,
NA, 16.7692401486971, 16.7692401486971, 22.8464274960424,
NA, 22.8464274960424, 42.8571212084921, 42.8571212084921,
NA, 58.7504956668303, NA, 58.7504956668303, 68.6330215319166,
68.6330215319166, NA), usgs_price = c(NA, 45.5323791503906,
44.8896598815918, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA), multicolour_price = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), comtrade_value = c(76133440, NA, NA, 62305808,
62305808, 1091597824, 417448928, 66877496, 1171693696, 448079232,
66877496, 76446016, 512188288, 1339334144, 76446016, 1643753856,
628604480, 418313248, 1093857920, 529067712, 1383472512,
717797248, 1876986240, 748939840, NA, NA, 1958421888, 877859392,
847055424, 365471232, NA, 657848192, 426205312, NA, 255723184,
NA, 168767856, 303782144, 536430400, NA, 298016896, 912430656,
506905920, NA, 593088768, NA, 1067559744, 1312398336, 729110208,
NA), wb_value = c(64626612, NA, NA, 58750496, 58750496, 1029308672,
393628320, 68633024, 1202450560, 459841248, 68633024, 73954616,
495495904, 1295684864, 73954616, 1733089280, 662768192, 418824960,
1095196032, 529573184, 1384794368, 682766400, 1785383168,
676864192, NA, NA, 1769949312, 915343488, 969826176, 404228960,
NA, 727612096, 497491968, NA, 298495168, NA, 251538608, 452769472,
616853568, NA, 342696416, 1157142272, 642856832, NA, 881257408,
NA, 1586263424, 1853091584, 1029495296, NA), usgs_value = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), multicolour_value = c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), yearvalue = c(76133438.9648438,
NA, NA, 62305808.59375, 62305808.59375, 1091597766.5625,
417448917.578125, 66877492.1875, 1171693663.125, 448079197.65625,
66877492.1875, 76446015.625, 512188304.6875, 1339334193.75,
76446015.625, 1643753775, 628604468.75, 418313269.921875,
1093857983.4375, 529067698.4375, 1383472548.75, 717797224.21875,
1876986174.375, 748939871.09375, NA, NA, 1958421871.875,
877859388, 847055412, 365471220, NA, 657848196, 426205320,
NA, 255723192, NA, 168767865, 303782157, 536430384, NA, 298016880,
912430656, 506905920, NA, 593088720, NA, 1067559696, 1312398288,
729110160, NA), price_mult = c(14501.607421875, NA, NA, 62305.80859375,
62305.80859375, 62305.80859375, 62305.80859375, 66877.4921875,
66877.4921875, 66877.4921875, 66877.4921875, 76446.015625,
76446.015625, 76446.015625, 76446.015625, 93821.5625, 93821.5625,
62434.81640625, 62434.81640625, 78965.328125, 78965.328125,
107133.9140625, 107133.9140625, 111782.0703125, 111782.0703125,
111782.0703125, 111782.0703125, 20901414, 20167986, 24364748,
NA, 24364748, 21310266, NA, 21310266, NA, 11251191, 11251191,
19867792, NA, 19867792, 33793728, 33793728, NA, 39539248,
NA, 39539248, 48607344, 48607344, NA), oil_gas = c(1, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1), gold = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0), artisanal = c(0, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0)), row.names = c(NA, 50L), class = "data.frame")
I would like to know how many distinct values there are for the variables comtrade_price_mult, wb_price_mult, usgs_price_mult
and multicolour_price_mult
for each unique combination of NAME_0, year, standardmeasure, resource
.
The result should look something like:
year NAME_0 standardmeasure resource n_comtrade n_wb n_usgs n_multicolour
2004 Ghana kilogram gold 3 1 1 NA
2005 Namibia metric tons diamonds 2 1 NA 1
with NA indicating that there are only NA values.
Maybe through some combination of n_distinct() and group_by() using a pipe?
CodePudding user response:
Using group_by
, summarise
, ... you could do:
Note: I use an if
to return NA
if there are only NA
values. Also note that if you don't want to count NAs than you have to add na.rm = TRUE
to n_distinct
.
library(dplyr)
df |>
group_by(year, NAME_0, standardmeasure, resource) |>
summarise(across(c(comtrade_price_mult, wb_price_mult, usgs_price_mult, multicolour_price_mult), ~ if (all(is.na(.x))) NA_integer_ else n_distinct(.x)), .groups = "drop") |>
rename_with(~ gsub("^(.*?)_price_mult$", "n_\\1", .x), .cols = ends_with("price_mult"))
#> # A tibble: 27 × 8
#> year NAME_0 standardmeasure resource n_comtrade n_wb n_usgs n_multicolour
#> <dbl> <chr> <chr> <chr> <int> <int> <int> <int>
#> 1 1994 Egypt million 42-gallo… oil 1 1 NA NA
#> 2 1995 Egypt million 42-gallo… oil 1 1 NA NA
#> 3 1996 Egypt million 42-gallo… oil 1 1 NA NA
#> 4 1996 Egypt million cubic me… natural… NA NA NA NA
#> 5 1997 Egypt million 42-gallo… oil 1 1 NA NA
#> 6 1997 Egypt million cubic me… natural… NA NA NA NA
#> 7 1998 Egypt million 42-gallo… oil 1 1 NA NA
#> 8 1998 Egypt million cubic me… natural… NA NA NA NA
#> 9 1999 Egypt million 42-gallo… oil 1 1 NA NA
#> 10 1999 Egypt million cubic me… natural… NA NA NA NA
#> # … with 17 more rows