-1
$\begingroup$

Given the following datasets:

rawdataNew1 = {{"time", "subregion", "country", "status", 
"consumption", "exports", "government exp", "real GDP", "cpi", 
"production index"}, {2020.`, "South Asia", "Afghanistan", "LDC", 
25.`, 25.`, 25.`, 25.`, 28.8`, 27.048600499362`}, {2020.`, 
"South Asia", "Bangladesh", "LDC", 79.2`, 75.2`, 16.`, 15.`, 
9.816957121888048`, 9.556161122013`}, {2020.`, "South Asia", 
"Bhutan", "LDC", 65.8`, 78.4`, 43.`, 34.`, 28.1063370463385`, 
29.498494702024`}, {2020.`, "South Asia", "India", "ODC", 65.7`, 
72.3`, 22.`, 18.`, 19.828018499703`, 18.727411344017`}, {2020.`, 
"South Asia", "Maldives", "ODC", 5.`, 61.4`, 25.`, 69.`, 
25.676828054044`, 26.39690268758`}, {2020.`, "South Asia", "Nepal",
"LDC", 88.5`, 81.`, 10.`, 9.`, 22.25877653822`, 
21.923054833825`}, {2020.`, "South Asia", "Pakistan", "ODC", 90.`, 
94.6`, 14.`, 10.`, 14.101228347339`, 15.113000954329`}, {2020.`, 
"South Asia", "Sri Lanka", "ODC", 76.9`, 78.7`, 20.`, 23.`, 
13.085048793291001`, 9.622172267201`}, {2020.`, "Southeast Asia", 
"Brunei Darussalam", "ODC", 36.9`, 45.5`, 67.`, 58.`, 
25.690233099619`, 8.8065584881334`}, {2020.`, "Southeast Asia", 
"Cambodia", "LDC", 87.6`, 74.2`, 54.`, 61.`, 25.4990651142935`, 
22.473359175818`}, {2020.`, "Southeast Asia", "Indonesia", "ODC", 
65.2`, 66.7`, 24.`, 18.`, 14.5172000188245`, 
12.364844516393`}, {2020.`, "Southeast Asia", "Laos", "LDC", 86.5`,
 25.`, 35.`, 25.`, 15.8137596099375`, 12.054639364958`}, {2020.`, 
"Southeast Asia", "Malaysia", "ODC", 60.7`, 71.5`, 87.`, 65.`, 
20.7378499035525`, 20.357776189578`}, {2020.`, "Southeast Asia", 
"Myanmar (Burma)", "LDC", 67.3`, 70.4`, 20.`, 30.`, 
16.989257478684`, 16.037629612146`}, {2020.`, "Southeast Asia", 
"Philippines", "ODC", 79.9`, 85.7`, 33.`, 28.`, 19.6007801467695`, 
19.647786797883`}, {2020.`, "Southeast Asia", "Singapore", "ODC", 
46.`, 46.2`, 198.`, 174.`, 17.801460780012498`, 
17.691486935944`}, {2020.`, "Southeast Asia", "Thailand", "ODC", 
68.`, 66.2`, 66.`, 60.`, 21.209241317252`, 
20.604531723764`}, {2020.`, "Southeast Asia", 
"Timor-Leste (East Timor)", "LDC", 174.6`, 101.8`, 9.`, 28.`, 
51.011741055190996`, 50.817786342346`}, {2020.`, "Southeast Asia", 
"Vietnam", "ODC", 72.6`, 74.6`, 72.`, 107.`, 19.515540437329`, 
16.171258043431`}, {2020.`, "Eastern Asia", "China", "ODC", 25.`, 
25.`, 25.`, 25.`, 28.024850206732502`, 25.592166507773`}, {2020.`, 
"Eastern Asia", "Japan", "ODC", 77.2`, 75.4`, 15.`, 19.`, 
34.202910551386495`, 34.050113431921`}, {2020.`, "Eastern Asia", 
"Mongolia", "ODC", 67.9`, 66.8`, 47.`, 60.`, 31.806646933783497`, 
28.155524418943`}, {2020.`, "Eastern Asia", "North Korea", "ODC", 
25.`, 25.`, 25.`, 25.`, 25.`, 25.`}, {2020.`, "Eastern Asia", 
"South Korea", "ODC", 64.6`, 65.7`, 47.`, 40.`, 22.9669437944995`, 
22.826303441138`}, {2020.`, "SIDS", "American Samoa", "LDC", 25.`, 
25.`, 63.`, 69.`, 25.`, 25.`}, {2020.`, "SIDS", "Fiji", "ODC", 
87.3`, 80.2`, 57.`, 48.`, 26.207533616694498`, 
20.495646073594`}, {2020.`, "SIDS", "French Polynesia", "ODC", 
25.`, 25.`, 25.`, 25.`, 25.`, 25.`}, {2020.`, "SIDS", "Guam", 
"ODC", 25.`, 25.`, 17.`, 20.`, 25.`, 25.`}, {2020.`, "SIDS", 
"Hong Kong SAR, China", "ODC", 25.`, 25.`, 25.`, 25.`, 
20.479931410596002`, 19.675156944555`}, {2020.`, "SIDS", 
"Kiribati", "LDC", 151.3`, 150.7`, 12.`, 10.`, 127.60830739469`, 
104.92502571463`}, {2020.`, "SIDS", "Marshall Islands", "ODC", 
25.`, 25.`, 25.`, 25.`, 62.258733961046005`, 
66.472089105629`}, {2020.`, "SIDS", "Micronesia", "ODC", 25.`, 
25.`, 24.`, 35.`, 77.6519950815505`, 66.200820487362`}, {2020.`, 
"SIDS", "Nauru", "ODC", 25.`, 25.`, 25.`, 25.`, 
137.10787328369003`, 166.03066370088`}, {2020.`, "SIDS", 
"New Caledonia", "ODC", 25.`, 25.`, 25.`, 25.`, 25.`, 
25.`}, {2020.`, "SIDS", "Northern Maria Islands", "ODC", 25.`, 
25.`, 25.`, 25.`, 25.`, 25.`}, {2020.`, "SIDS", "Palau", "ODC", 
106.2`, 103.6`, 49.`, 48.`, 43.937590802136`, 
42.766858440495`}, {2020.`, "SIDS", "Papua New Guinea", "ODC", 
25.`, 25.`, 25.`, 25.`, 17.002268918370497`, 
13.917009141834`}, {2020.`, "SIDS", "Samoa", "ODC", 25.`, 25.`, 
28.`, 37.`, 33.8866916421535`, 38.486285723977`}, {2020.`, "SIDS", 
"Solomon Islands", "LDC", 25.`, 25.`, 40.`, 25.`, 
36.5675228455985`, 31.353772203441`}, {2020.`, "SIDS", "Tonga", 
"ODC", 116.9`, 25.`, 13.`, 21.`, 42.1411902303165`, 
45.121161008633`}, {2020.`, "SIDS", "Tuvalu", "LDC", 25.`, 25.`, 
25.`, 25.`, 145.083027264305`, 131.81684280884`}, {2020.`, "SIDS", 
"Vanuatu", "LDC", 79.5`, 25.`, 47.`, 25.`, 41.1807536476675`, 
44.808455359488`}, {2020.`, "Pacific", "Australia", "ODC", 74.2`, 
74.3`, 20.`, 24.`, 35.0470091855735`, 35.032340730559`}, {2020.`, 
"Pacific", "New Zealand", "ODC", 77.6`, 75.9`, 30.`, 28.`, 
36.928656157283`, 36.724972823768`}}

rawdataNew2 = {{"time", "subregion", "country", "status", 
   "consumption", "exports", "government exp", "real GDP", "cpi", 
   "production index"}, {2019.`, "South Asia", "Afghanistan", "LDC", 
   10.`, 10.`, 10.`, 10.`, 10.`, 27.048600499362`}, {2019.`, 
   "South Asia", "Bangladesh", "LDC", 79.2`, 75.2`, 16.`, 15.`, 
   9.816957121888048`, 9.556161122013`}, {2019.`, "South Asia", 
   "Bhutan", "LDC", 65.8`, 78.4`, 43.`, 34.`, 28.1063370463385`, 
   29.498494702024`}, {2019.`, "South Asia", "India", "ODC", 65.7`, 
   72.3`, 22.`, 18.`, 19.828018499703`, 18.727411344017`}, {2019.`, 
   "South Asia", "Maldives", "ODC", 10.`, 10.`, 10.`, 69.`, 
   25.676828054044`, 26.39690268758`}, {2019.`, "South Asia", "Nepal",
    "LDC", 88.5`, 81.`, 10.`, 9.`, 22.25877653822`, 
   21.923054833825`}, {2019.`, "South Asia", "Pakistan", "ODC", 90.`, 
   94.6`, 14.`, 10.`, 14.101228347339`, 15.113000954329`}, {2019.`, 
   "South Asia", "Sri Lanka", "ODC", 76.9`, 78.7`, 20.`, 23.`, 
   13.085048793291001`, 9.622172267201`}, {2019.`, "Southeast Asia", 
   "Brunei Darussalam", "ODC", 36.9`, 45.5`, 67.`, 58.`, 
   25.690233099619`, 8.8065584881334`}, {2019.`, "Southeast Asia", 
   "Cambodia", "LDC", 87.6`, 74.2`, 54.`, 61.`, 25.4990651142935`, 
   22.473359175818`}, {2019.`, "Southeast Asia", "Indonesia", "ODC", 
   65.2`, 66.7`, 24.`, 18.`, 14.5172000188245`, 
   12.364844516393`}, {2019.`, "Southeast Asia", "Laos", "LDC", 86.5`,
    10.`, 35.`, 10.`, 15.8137596099375`, 12.054639364958`}, {2019.`, 
   "Southeast Asia", "Malaysia", "ODC", 60.7`, 71.5`, 87.`, 10.`, 
   20.7378499035525`, 20.357776189578`}, {2019.`, "Southeast Asia", 
   "Myanmar (Burma)", "LDC", 67.3`, 70.4`, 20.`, 10.`, 
   16.989257478684`, 16.037629612146`}, {2019.`, "Southeast Asia", 
   "Philippines", "ODC", 79.9`, 85.7`, 33.`, 10.`, 19.6007801467695`, 
   19.647786797883`}, {2019.`, "Southeast Asia", "Singapore", "ODC", 
   46.`, 46.2`, 198.`, 10.`, 17.801460780012498`, 
   17.691486935944`}, {2019.`, "Southeast Asia", "Thailand", "ODC", 
   68.`, 66.2`, 66.`, 10.`, 21.209241317252`, 
   20.604531723764`}, {2019.`, "Southeast Asia", 
   "Timor-Leste (East Timor)", "LDC", 174.6`, 101.8`, 9.`, 10.`, 
   51.011741055190996`, 50.817786342346`}, {2019.`, "Southeast Asia", 
   "Vietnam", "ODC", 72.6`, 74.6`, 72.`, 10.`, 19.515540437329`, 
   16.171258043431`}, {2019.`, "Eastern Asia", "China", "ODC", 10.`, 
   10.`, 10.`, 10.`, 28.024850206732502`, 25.592166507773`}, {2019.`, 
   "Eastern Asia", "Japan", "ODC", 10.`, 10.`, 15.`, 19.`, 
   34.202910551386495`, 34.050113431921`}, {2019.`, "Eastern Asia", 
   "Mongolia", "ODC", 10.`, 10.`, 47.`, 60.`, 31.806646933783497`, 
   28.155524418943`}, {2019.`, "Eastern Asia", "North Korea", "ODC", 
   10.`, 10.`, 10.`, 10.`, 10.`, 10.`}, {2019.`, "Eastern Asia", 
   "South Korea", "ODC", 10.`, 10.`, 47.`, 40.`, 22.9669437944995`, 
   22.826303441138`}, {2019.`, "SIDS", "American Samoa", "LDC", 10.`, 
   10.`, 63.`, 69.`, 20.`, 20.`}, {2019.`, "SIDS", "Fiji", "ODC", 
   10.`, 10.`, 57.`, 48.`, 26.207533616694498`, 20.`}, {2019.`, 
   "SIDS", "French Polynesia", "ODC", 10.`, 10.`, 10.`, 10.`, 20.`, 
   20.`}, {2019.`, "SIDS", "Guam", "ODC", 10.`, 10.`, 10.`, 10.`, 
   20.`, 20.`}, {2019.`, "SIDS", "Hong Kong SAR, China", "ODC", 10.`, 
   10.`, 10.`, 10.`, 20.479931410596002`, 19.675156944555`}, {2019.`, 
   "SIDS", "Kiribati", "LDC", 151.3`, 150.7`, 10.`, 10.`, 
   127.60830739469`, 104.92502571463`}, {2019.`, "SIDS", 
   "Marshall Islands", "ODC", 20.`, 20.`, 10.`, 10.`, 
   62.258733961046005`, 66.472089105629`}, {2019.`, "SIDS", 
   "Micronesia", "ODC", 20.`, 20.`, 10.`, 10.`, 77.6519950815505`, 
   66.200820487362`}, {2019.`, "SIDS", "Nauru", "ODC", 20.`, 20.`, 
   10.`, 10.`, 137.10787328369003`, 166.03066370088`}, {2019.`, 
   "SIDS", "New Caledonia", "ODC", 20.`, 20.`, 10.`, 10.`, 20.`, 
   20.`}, {2019.`, "SIDS", "Northern Maria Islands", "ODC", 20.`, 
   20.`, 10.`, 10.`, 20.`, 20.`}, {2019.`, "SIDS", "Palau", "ODC", 
   106.2`, 103.6`, 49.`, 48.`, 43.937590802136`, 
   42.766858440495`}, {2019.`, "SIDS", "Papua New Guinea", "ODC", 
   20.`, 20.`, 20.`, 20.`, 17.002268918370497`, 
   13.917009141834`}, {2019.`, "SIDS", "Samoa", "ODC", 20.`, 20.`, 
   28.`, 37.`, 33.8866916421535`, 38.486285723977`}, {2019.`, "SIDS", 
   "Solomon Islands", "LDC", 20.`, 20.`, 40.`, 20.`, 
   36.5675228455985`, 31.353772203441`}, {2019.`, "SIDS", "Tonga", 
   "ODC", 116.9`, 20.`, 13.`, 20.`, 42.1411902303165`, 
   45.121161008633`}, {2019.`, "SIDS", "Tuvalu", "LDC", 20.`, 20.`, 
   20.`, 20.`, 145.083027264305`, 131.81684280884`}, {2019.`, "SIDS", 
   "Vanuatu", "LDC", 79.5`, 20.`, 47.`, 20.`, 41.1807536476675`, 
   44.808455359488`}, {2019.`, "Pacific", "Australia", "ODC", 74.2`, 
   74.3`, 20.`, 24.`, 35.0470091855735`, 35.032340730559`}, {2019.`, 
   "Pacific", "New Zealand", "ODC", 77.6`, 75.9`, 30.`, 28.`, 
   36.928656157283`, 36.724972823768`}}

In this link, the answer is already given but with a smaller number of identifiers. In the above datasets, I have 4 identifiers: {time, subregion, status, country} across 6 indicators (or variables) across two years. I like to extend the Manipulate given in the link to also show subregion and status so that I can pick a specific subregion and plot those countries in the subregion only. Or I can select a status and pick those countries to draw the variables.

The following code produces the combined data set:

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

groupeddataNew = 
 jdataNew[GroupBy[#subregion &], GroupBy[#country &], 
  GroupBy[#time &], 
  All, {"consumption", "exports", "government exp", "real GDP", "cpi",
    "production index"}]

When I include the identifier GroupBy[#status &], the grouping does not work as seen from groupeddataNew. My purpose is to have a correctly structured data set and use it in Manipulate to produce charts and graphs for all the countries in a chosen subregion or in a chosen status group, while at the same time having the freedom to choose individual countries at will. The difficulty is that if a subregion is chosen, the selection of individual countries is irrelevant as the identifiers should be mutually exclusive.

$\endgroup$
1
  • $\begingroup$ @WReach: I edited the question with the definition of jdataNew. FYI. $\endgroup$ Commented Oct 30, 2021 at 18:58

1 Answer 1

2
+50
$\begingroup$

You may use Query and Select to filter the data by the identifiers.

With jdataNew in the OP the selection lists are created as

{statusList, subregionList} = 
    (key |-> Normal[Query[DeleteDuplicates, Slot[key] &]@jdataNew]) /@ {"status", "subregion"}
{{"LDC", "ODC"}, {"South Asia", "Southeast Asia", "Eastern Asia", "SIDS", "Pacific"}}

and

indicatorList = Normal[Query[First /* Keys]@jdataNew][[5 ;;]]
{"consumption", "exports", "government exp", "real GDP", "cpi", "production index"}

Then in Manipulate construct a Query that combines the Select from each list with RightComposition to filter the rows. GroupBy is used as before with the addition of swapping the order of the GroupBy pair by Transpose. Also, the country list is Dynamicly updated based on the status and subregion selection and refreshed on each selection by TrackingFunction.

Manipulate[
 statusFilter = Switch[status
   , All, True &
   , _, #"status" == status &
   ]
 ; subregionFilter = Switch[subregion
   , All, True &
   , _, #"subregion" == subregion &
   ]
 ; countryFilter = Switch[country
   , All, True &
   , _, #"country" == country &
   ]
 ; data =
  Query[
    Select[statusFilter] /*
     Select[subregionFilter] /*
     Select[countryFilter] /*
     GroupBy[#"country" &]
    , GroupBy[#"time" &]
    , First
    , Evaluate[Slot[indicator]] &
    ]@jdataNew
 ; BarChart[swapGrouing@data
  , ChartLabels -> {(Rotate[#, π/2] & /@ 
      Normal[Keys@swapGrouing@data]), None}
  , ChartLegends -> Automatic
  , ImageSize -> Large
  , PlotLabel -> 
   StringTemplate[
     "``: Region \[Rule] ``, Status \[Rule] ``, Country \[Rule] ``"][
    indicator, subregion, status, country
    ]
  ]
 , {data, None}
 , {statusFilter, None}
 , {{status, All, "Status"}
  , Prepend[All]@statusList
  , ControlType -> SetterBar
  , TrackingFunction -> (country = All; status = #; &)
  }
 , {subregionFilter, None}
 , {{subregion, All, "Subregion"}
  , Prepend[All]@subregionList
  , ControlType -> PopupMenu
  , TrackingFunction -> (country = All; subregion = #; &)
  }
 , {countryFilter, None}
 , {{country, All, "Country"}
  , Dynamic[
   Prepend[All]@
    Normal@Query[
       Select[statusFilter] /*
        Select[subregionFilter] /*
        DeleteDuplicates
       , #"country" &
       ]@jdataNew
   ]
  , ControlType -> PopupMenu
  }
 , {{indicator, First@indicatorList, "Indicator"}, indicatorList, ControlType -> PopupMenu}
 , {{swapGrouing, Identity, "Swap grouping"}, {Identity, Transpose}, ControlType -> Checkbox}
 ]

enter image description here

I leave the fancy formatting of the chart to you.

Hope this helps.

$\endgroup$
4
  • 1
    $\begingroup$ @TugrulTemel Yes, you can easily modify it by using CheckboxBar for the ControlType and MemberQ for the filter. $\endgroup$
    – Edmund
    Commented Nov 1, 2021 at 23:02
  • 1
    $\begingroup$ @TugrulTemel You must also change the construction of the filters to use MemberQ. $\endgroup$
    – Edmund
    Commented Nov 2, 2021 at 12:42
  • 1
    $\begingroup$ Include your filter using MemberQ below. Let's see where you went wrong. $\endgroup$
    – Edmund
    Commented Nov 2, 2021 at 14:12
  • $\begingroup$ Let us continue this discussion in chat. $\endgroup$
    – Edmund
    Commented Nov 2, 2021 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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