1
$\begingroup$

I have this data set:

rawdata = {{"time", "region", "subregion", "status", "country", 
"indicator", "data"}, {2000.`, "Africa", "Central Africa", "LDC", 
"Angola", "Remittances", 0.0267993914860461`}, {2000.`, "Africa", 
"Central Africa", "LDC", "Angola", "ODA", 
4.05719895987582`}, {2000.`, "Asia", "South Asia", "ODC", 
"Bhutan", "Tax revenues", 12.9794732034214`}, {2000.`, "Asia", 
"South Asia", "ODC", "Bhutan", "Gross domestic savings", 
27.3167848363906`}, {2000.`, "Asia", "South Asia", "ODC", 
"Bhutan", "Adjusted net savings", 44.1909199162357`}, {2000.`, 
"Asia", "SIDS", "LDC", "Solomon Islands", 
"Gross domestic savings", 0.221702694892883`}, {2000.`, "Asia", 
"SIDS", "LDC", "Kiribati", "Adjusted net savings", 7.9`}, {2001.`,
 "Africa", "Central Africa", "LDC", "Angola", "Remittances", 
0.0212549938881954`}, {2001.`, "Africa", "Central Africa", "LDC", 
"Angola", "ODA", 3.83358753594609`}, {2001.`, "Asia", 
"South Asia", "ODC", "Bhutan", "Tax revenues", 
12.5415621434496`}, {2001.`, "Asia", "South Asia", "ODC", 
"Bhutan", "Gross domestic savings", 37.1804901737896`}, {2001.`, 
"Asia", "South Asia", "ODC", "Bhutan", "Broadband subscriptions", 
0.497992844`}, {2001.`, "Asia", "SIDS", "LDC", "Solomon Islands", 
"Tax revenues", 4.7808499936`}, {2001.`, "Asia", "SIDS", "LDC", 
"Solomon Islands", "Gross domestic savings", 
0.38613349199295`}, {2001.`, "Asia", "SIDS", "LDC", "Kiribati", 
"Gross domestic savings", 7.9`}, {2001.`, "Asia", "SIDS", "LDC", 
"Kiribati", "Internet users", 8.6`}};

and

jdata = Join @@ 
Map[a \[Function] 
 Dataset[AssociationThread[First@a, #] & /@ Rest[a]]][{rawdata}];

generates:

enter image description here

I want to calculate "percentage change" for a given country and a given indicator over the beginning and end years available. For example, for Bhutan percentage change in Tax revenues over period 2000-2001.

I tried other operations such as below:

jdata[All, {"time", "country", "data"}][All, "data"] // Mean  

which is the mean value of all the data regardless of country and indicator.

This also gives me a result:

jdata[GroupBy["country"], Total[#] &, "data"]

which is the total of data (regardless of indicator) over the period 2000-2001. The percentage change calculation for individual countries should have the form:

jdata[GroupBy["country"], PercentageChange[Log[#2]-Log[#1]] &, "data"]

I cannot figure out how to do the percentage change in an indicator of a given country during a specific period of time (in my original data set, the time goes from 2000 to 2030), and in fact, I want to be able to select a subset within the entire time period.

EDIT

enter image description here

$\endgroup$
2
$\begingroup$

Something like this?

jdata[GroupBy[{#country, #indicator} & -> Last]] // 
  Map[Quantity[100.*(Last@# - First@#)/First@#, "Percent"] &]

enter image description here

Should add jdata // SortBy[#time &] so the first and last values are correct. For specific date ranges add e.g. Select[Between[#time, {2000, 2011}] &]

$\endgroup$
4
  • $\begingroup$ I edited my question because I noticed that my data set is not the one I originally posted. Please see the edit for the dataset format. I want to calculate for example percentage change in indicator "EVI" and "Export concentration" and so on. Sorry for my mistake. I just gave a small part of the dataset. $\endgroup$ Nov 13 '21 at 0:56
  • $\begingroup$ The question is to calculate percentage change for a given country, for a given time period, for a selected indicator like "EVI". Of course, calculations should be made for all countries at once. $\endgroup$ Nov 13 '21 at 1:02
  • $\begingroup$ Can you show me how to use Select[Between[#time, {2000, 2011}] &] in the code you gave above? $\endgroup$ Nov 13 '21 at 16:48
  • 1
    $\begingroup$ Perform the Select before the GroupBy. jdata[Select[Between[#time, {2000, 2011}] &]][ GroupBy[{#country, #indicator} & -> Last]] // Map[Quantity[100.*(Last@# - First@#)/First@#, "Percent"] &] $\endgroup$ Nov 14 '21 at 1:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.