Hi Florian (Tester Trying):
I would agree with Rohit on needed clarifications. The initial source data file and UN data in csv format need cleaning. The data files contain some incongruences pertaining to WolframData and the nature of country names over time and multiple regions with undefined sub-regions. As per your title, some pivoting on the M49 code helps. The following code is much the same as before up to UNcountryprop
where we diverge away from Association
and Dataset
and just manipulate list. Instead of running through each line of the "master.csv" file and adding the two elements (columns) {"continent", "region"}
it helps to optimize by reducing to a set of distinct countries, then mapping {"continent", "region"}
Clear[master, mastercountriesraw, UNmethoddata, addcountryprop, UNcountryprop, addcontinentandregionforexport, masterout, headings];
master = Import["C:\\suicide-rates-overview-1985-to-2016\\master.csv"];
mastercountriesraw = DeleteDuplicates[Rest[master[[All,1]]]];
UNmethoddata = Import["C:\\suicide-rates-overview-1985-to-2016\\UNSD MethodologyFixed.csv"];
addcountryprop[cnty_] := Module[
{ countryraw, countryent, continent, unnumber, globalcode, globalname, regioncode, regionname, subregioncode, subregionname, intermediateregioncode, intermediateregionname, countryorarea, M49code, bdata},
countryraw = cnty;
countryent = Quiet[CountryData[cnty]];
If[ToString[Head[countryent]] == "CountryData", countryent = Interpreter["Country"][cnty]];
continent = CanonicalName[CountryData[countryent, "Continent"]];
unnumber = CountryData[countryent, "UNNumber"];
{ globalcode, globalname, regioncode, regionname, subregioncode, subregionname, intermediateregioncode, intermediateregionname, countryorarea, M49code } =
Take[SelectFirst[UNmethoddata, (#[[10]] == unnumber) &], 10];
bdata = Select[master, (#[[1]] == countryraw) &];
{ globalname, regionname, subregionname, If[intermediateregionname == "", subregionname, intermediateregionname], continent, countryraw, Rest /@ bdata }
];
UNcountryprop = Map[addcountryprop[#] &, mastercountriesraw];
addcontinentandregionforexport[item_] := Module[
{ contiregcountr },
contiregcountr = {item[[5]], item[[4]], item[[6]]};
Map[Join[contiregcountr, #] &, item[[7]] ]
];
masterout = Flatten[Map[addcontinentandregionforexport[#] &, UNcountryprop], 1];
headings = Join[ {"continent","region"}, First[master] ];
Export["C:\\suicide-rates-overview-1985-to-2016\\masterout.csv", masterout, "CSV", "TableHeadings" -> headings]
In reviewing previous code I spotted some issues with the GroupBy
; but you don't want a Dataset
.Using the Wolfram Language to add two columns to a CSV file is actually straight forward. There are some complications as to how to add those columns in your post questions.