1
$\begingroup$

In my earlier question (in this link) the given database is nicely formatted with columns denoting data for variables across countries and years.

But, data sets are not always nicely formatted. Here is an unformatted database (I created for minimal exposition to formulate my question) in which all the data are presented in a single column, as well as variable names repeated in another single column. Time periods across variables and countries are not identical. For one country, the available data may extend from 2001 to 2020 but for another country, the same variable may be short-lived from 2005 to 2015. Furthermore, countries are grouped into two categories: LDC and ODC. Given all these limitations, I like to generate BarCharts and ListLinePlot as illustrated in the above link. I know that Manipulate is so powerful if it is used efficiently, and that I would like to have the charts in Manipulate as it allows for very easy scenarios of the parameters.

In the current database given below, there are four parameters: {country, status, vars, time} that will be used depending on the type of chart to be produced.

Example: Draw a single BarChart with all the countries and all years for a single (multiple) variable vars across country status (LDC and ODC). This is just an example of many others.

rawdata1 = {{"country", "status", "vars", "time", "data"}, {"Bhutan", 
   "LDC", "GDP growth", 2012.`, 11.7317902498375`}, {"Bhutan", "LDC", 
   "GDP growth", 2013.`, 7.89168738800201`}, {"Bhutan", "LDC", 
   "GDP growth", 2014.`, 5.07109529390251`}, {"Bhutan", "LDC", 
   "GDP growth", 2015.`, 2.14228177446305`}, {"Bhutan", "LDC", 
   "GDP growth", 2016.`, 3.99347434438733`}, {"Bhutan", "LDC", 
   "GDP growth", 2017.`, 6.64235386538354`}, {"Bhutan", "LDC", 
   "GDP growth", 2018.`, 8.1271556376086`}, {"Bhutan", "LDC", 
   "GDP growth", 2019.`, 4.65170017994314`}, {"Bhutan", "LDC", 
   "GDP growth", 2020.`, 3.02767016810783`}, {"Bhutan", "LDC", 
   "GDP growth", 2021.`, 3.80610777007113`}, {"Bhutan", "LDC", 
   "GDP shocks", 2009.`, 0.740265356463854`}, {"Bhutan", "LDC", 
   "GDP shocks", 2010.`, 0.740265356463854`}, {"Bhutan", "LDC", 
   "GDP shocks", 2011.`, 0.740265356463854`}, {"Bhutan", "LDC", 
   "GDP shocks", 2012.`, 0.740265356463854`}, {"Bhutan", "LDC", 
   "GDP shocks", 2013.`, 2.01311158452329`}, {"Bhutan", "LDC", 
   "GDP shocks", 2014.`, 2.01311158452329`}, {"Bhutan", "LDC", 
   "GDP shocks", 2015.`, 2.14228177446305`}, {"Bhutan", "LDC", 
   "GDP shocks", 2016.`, 2.14228177446305`}, {"Bhutan", "LDC", 
   "GDP shocks", 2017.`, 2.14228177446305`}, {"Bhutan", "LDC", 
   "GDP shocks", 2018.`, 2.14228177446305`}, {"Bhutan", "LDC", 
   "External debt", 2008.`, 82.7500899512726`}, {"Bhutan", "LDC", 
   "External debt", 2009.`, 69.5307200123119`}, {"Bhutan", "LDC", 
   "External debt", 2010.`, 58.0567671342573`}, {"Bhutan", "LDC", 
   "External debt", 2011.`, 66.462854452616`}, {"Bhutan", "LDC", 
   "External debt", 2012.`, 64.0152496294596`}, {"Bhutan", "LDC", 
   "External debt", 2013.`, 64.3635506717257`}, {"Bhutan", "LDC", 
   "External debt", 2014.`, 88.4379637469247`}, {"Bhutan", "LDC", 
   "External debt", 2015.`, 93.5919296023436`}, {"Bhutan", "LDC", 
   "External debt", 2018.`, 116.021645859231`}, {"Bhutan", "LDC", 
   "External debt", 2019.`, 116.55424756929`}, {"Bhutan", "LDC", 
   "External debt", 2020.`, 114.619068673739`}, {"Bhutan", "LDC", 
   "Debt servicing", 2007.`, 2.79220345990346`}, {"Bhutan", "LDC", 
   "Debt servicing", 2008.`, 4.84585842175085`}, {"Bhutan", "LDC", 
   "Debt servicing", 2009.`, 11.8507099824423`}, {"Bhutan", "LDC", 
   "Debt servicing", 2010.`, 12.6844892415372`}, {"Bhutan", "LDC", 
   "Debt servicing", 2016.`, 17.2355189434556`}, {"Bhutan", "LDC", 
   "Debt servicing", 2017.`, 16.5847794392432`}, {"Bhutan", "LDC", 
   "Debt servicing", 2018.`, 11.0524403566625`}, {"Bhutan", "LDC", 
   "Debt servicing", 2019.`, 10.718740205243`}, {"Bhutan", "LDC", 
   "Debt servicing", 2020.`, 7.51326635700829`}, {"Bangladesh", "ODC",
    "GDP growth", 2013.`, 6.4643849452773`}, {"Bangladesh", "ODC", 
   "GDP growth", 2014.`, 6.52145155902684`}, {"Bangladesh", "ODC", 
   "GDP growth", 2015.`, 6.01361059194194`}, {"Bangladesh", "ODC", 
   "GDP growth", 2016.`, 6.06107852277686`}, {"Bangladesh", "ODC", 
   "GDP growth", 2017.`, 6.55263331602802`}, {"Bangladesh", "ODC", 
   "GDP growth", 2018.`, 7.1134894741232`}, {"Bangladesh", "ODC", 
   "GDP growth", 2019.`, 7.28418409195113`}, {"Bangladesh", "ODC", 
   "GDP growth", 2020.`, 7.86371944223927`}, {"Bangladesh", "ODC", 
   "GDP growth", 2021.`, 8.15268414939789`}, {"Bangladesh", "ODC", 
   "GDP shocks", 2011.`, 3.39981548740751`}, {"Bangladesh", "ODC", 
   "GDP shocks", 2012.`, 3.39981548740751`}, {"Bangladesh", "ODC", 
   "GDP shocks", 2013.`, 4.08468894261222`}, {"Bangladesh", "ODC", 
   "GDP shocks", 2014.`, 4.08468894261222`}, {"Bangladesh", "ODC", 
   "GDP shocks", 2015.`, 4.08468894261222`}, {"Bangladesh", "ODC", 
   "GDP shocks", 2016.`, 4.41555612209166`}, {"Bangladesh", "ODC", 
   "GDP shocks", 2017.`, 4.41555612209166`}, {"Bangladesh", "ODC", 
   "GDP shocks", 2018.`, 4.41555612209166`}, {"Bangladesh", "ODC", 
   "GDP shocks", 2019.`, 4.41555612209166`}, {"Bangladesh", "ODC", 
   "GDP shocks", 2020.`, 4.41555612209166`}, {"Bangladesh", "ODC", 
   "GDP shocks", 2021.`, 4.41555612209166`}, {"Bangladesh", "ODC", 
   "External debt", 2006.`, 29.0231053050116`}, {"Bangladesh", "ODC", 
   "External debt", 2007.`, 25.5142249883711`}, {"Bangladesh", "ODC", 
   "External debt", 2012.`, 21.3186585394465`}, {"Bangladesh", "ODC", 
   "External debt", 2013.`, 19.4789273407571`}, {"Bangladesh", "ODC", 
   "External debt", 2014.`, 19.5470222088404`}, {"Bangladesh", "ODC", 
   "External debt", 2015.`, 19.439616467771`}, {"Bangladesh", "ODC", 
   "External debt", 2016.`, 17.7117967565762`}, {"Bangladesh", "ODC", 
   "External debt", 2017.`, 17.3096636682518`}, {"Bangladesh", "ODC", 
   "External debt", 2018.`, 16.430183438899`}, {"Bangladesh", "ODC", 
   "External debt", 2019.`, 17.9742504913686`}, {"Bangladesh", "ODC", 
   "External debt", 2020.`, 18.1937153154054`}, {"Bangladesh", "ODC", 
   "External debt", 2021.`, 18.0142002753455`}, {"Bangladesh", "ODC", 
   "Debt servicing", 2009.`, 5.31781131096202`}, {"Bangladesh", "ODC",
    "Debt servicing", 2010.`, 6.0412536184725`}, {"Bangladesh", "ODC",
    "Debt servicing", 2011.`, 5.08243215255821`}, {"Bangladesh", 
   "ODC", "Debt servicing", 2012.`, 5.62926552857404`}, {"Bangladesh",
    "ODC", "Debt servicing", 2013.`, 
   5.84313717722571`}, {"Bangladesh", "ODC", "Debt servicing", 2014.`,
    5.92461780359757`}, {"Bangladesh", "ODC", "Debt servicing", 
   2015.`, 5.87634917084482`}, {"Bangladesh", "ODC", "Debt servicing",
    2016.`, 4.65398848053981`}, {"Bangladesh", "ODC", 
   "Debt servicing", 2017.`, 4.63526201562755`}, {"Bangladesh", "ODC",
    "Debt servicing", 2018.`, 5.4542264908716`}, {"Bangladesh", "ODC",
    "Debt servicing", 2019.`, 6.44420242273907`}, {"Bangladesh", 
   "ODC", "Debt servicing", 2020.`, 12.8165870165076`}, {"Kiribati", 
   "LDC", "GDP growth", 2008.`, -0.0493465149077776`}, {"Kiribati", 
   "LDC", "GDP growth", 2009.`, 2.03503444919169`}, {"Kiribati", 
   "LDC", "GDP growth", 2010.`, -2.09164562868308`}, {"Kiribati", 
   "LDC", "GDP growth", 2011.`, 0.802961941412161`}, {"Kiribati", 
   "LDC", "GDP growth", 2012.`, -0.924067932533501`}, {"Kiribati", 
   "LDC", "GDP growth", 2016.`, -0.604818488715408`}, {"Kiribati", 
   "LDC", "GDP growth", 2017.`, 10.2959730488488`}, {"Kiribati", 
   "LDC", "GDP growth", 2018.`, 1.14025038951184`}, {"Kiribati", 
   "LDC", "GDP growth", 2019.`, 4.88310317936931`}, {"Kiribati", 
   "LDC", "GDP growth", 2020.`, 2.31308712014162`}, {"Kiribati", 
   "LDC", "GDP growth", 2021.`, 2.29661272883723`}, {"Kiribati", 
   "LDC", "GDP shocks", 2002.`, -9.80119924951005`}, {"Kiribati", 
   "LDC", "GDP shocks", 2011.`, -4.7598081474706`}, {"Kiribati", 
   "LDC", "GDP shocks", 2012.`, -4.7598081474706`}, {"Kiribati", 
   "LDC", "GDP shocks", 2013.`, -4.7598081474706`}, {"Kiribati", 
   "LDC", "GDP shocks", 2014.`, -4.7598081474706`}, {"Kiribati", 
   "LDC", "GDP shocks", 2015.`, -4.7598081474706`}, {"Kiribati", 
   "LDC", "GDP shocks", 2016.`, -4.7598081474706`}, {"Kiribati", 
   "LDC", "GDP shocks", 2017.`, -4.7598081474706`}, {"Kiribati", 
   "LDC", "GDP shocks", 2018.`, -4.7598081474706`}, {"Kiribati", 
   "LDC", "GDP shocks", 2019.`, -4.7598081474706`}, {"Kiribati", 
   "LDC", "GDP shocks", 2020.`, -4.7598081474706`}, {"Kiribati", 
   "LDC", "GDP shocks", 2021.`, -4.7598081474706`}, {"Nepal", "LDC", 
   "GDP growth", 2006.`, 4.68260324535137`}, {"Nepal", "LDC", 
   "GDP growth", 2007.`, 3.47918104631144`}, {"Nepal", "LDC", 
   "GDP growth", 2008.`, 3.36461478807166`}, {"Nepal", "LDC", 
   "GDP growth", 2009.`, 3.41156027569265`}, {"Nepal", "LDC", 
   "GDP growth", 2010.`, 6.10463914231689`}, {"Nepal", "LDC", 
   "GDP growth", 2011.`, 4.53307872039284`}, {"Nepal", "LDC", 
   "GDP growth", 2015.`, 4.12887767631092`}, {"Nepal", "LDC", 
   "GDP growth", 2016.`, 5.98893336810162`}, {"Nepal", "LDC", 
   "GDP growth", 2017.`, 3.32295600970052`}, {"Nepal", "LDC", 
   "GDP growth", 2018.`, 0.588654958809332`}, {"Nepal", "LDC", 
   "GDP growth", 2019.`, 8.2234809809647`}, {"Nepal", "LDC", 
   "GDP growth", 2020.`, 6.70103808547462`}, {"Nepal", "LDC", 
   "GDP growth", 2021.`, 6.99121243750931`}, {"Nepal", "LDC", 
   "GDP shocks", 2002.`, -2.9780114727446`}, {"Nepal", "LDC", 
   "GDP shocks", 2011.`, 0.119813643353961`}, {"Nepal", "LDC", 
   "GDP shocks", 2012.`, 0.119813643353961`}, {"Nepal", "LDC", 
   "GDP shocks", 2013.`, 0.119813643353961`}, {"Nepal", "LDC", 
   "GDP shocks", 2014.`, 0.119813643353961`}, {"Nepal", "LDC", 
   "GDP shocks", 2015.`, 0.119813643353961`}, {"Nepal", "LDC", 
   "GDP shocks", 2016.`, 0.119813643353961`}, {"Nepal", "LDC", 
   "GDP shocks", 2017.`, 0.119813643353961`}, {"Nepal", "LDC", 
   "GDP shocks", 2018.`, 0.119813643353961`}, {"Nepal", "LDC", 
   "GDP shocks", 2019.`, 0.119813643353961`}, {"Nepal", "LDC", 
   "GDP shocks", 2020.`, 0.119813643353961`}, {"Nepal", "LDC", 
   "GDP shocks", 2021.`, 0.119813643353961`}, {"Nepal", "LDC", 
   "External debt", 2009.`, 34.6388304814776`}, {"Nepal", "LDC", 
   "External debt", 2010.`, 29.1821447836312`}, {"Nepal", "LDC", 
   "External debt", 2013.`, 20.1190041573439`}, {"Nepal", "LDC", 
   "External debt", 2014.`, 20.0489422598981`}, {"Nepal", "LDC", 
   "External debt", 2015.`, 20.6639601633151`}, {"Nepal", "LDC", 
   "External debt", 2016.`, 19.5755300670239`}, {"Nepal", "LDC", 
   "External debt", 2017.`, 19.0433862315843`}, {"Nepal", "LDC", 
   "External debt", 2018.`, 19.9827561972183`}, {"Nepal", "LDC", 
   "External debt", 2019.`, 19.4856042520084`}, {"Nepal", "LDC", 
   "External debt", 2020.`, 18.7510220424003`}, {"Nepal", "LDC", 
   "External debt", 2021.`, 21.0138276442613`}, {"Tuvalu", "ODC", 
   "GDP growth", 2005.`, -3.10512510825693`}, {"Tuvalu", "ODC", 
   "GDP growth", 2006.`, -1.67649286925071`}, {"Tuvalu", "ODC", 
   "GDP growth", 2007.`, -4.11822919782043`}, {"Tuvalu", "ODC", 
   "GDP growth", 2008.`, 2.89081209141946`}, {"Tuvalu", "ODC", 
   "GDP growth", 2009.`, 6.34681136452036`}, {"Tuvalu", "ODC", 
   "GDP growth", 2010.`, 7.0953263780791`}, {"Tuvalu", "ODC", 
   "GDP growth", 2014.`, -3.88415250965769`}, {"Tuvalu", "ODC", 
   "GDP growth", 2015.`, 4.91003579781166`}, {"Tuvalu", "ODC", 
   "GDP growth", 2016.`, 1.17773912883179`}, {"Tuvalu", "ODC", 
   "GDP growth", 2017.`, 9.23046021376099`}, {"Tuvalu", "ODC", 
   "GDP growth", 2018.`, 5.88457557563717`}, {"Tuvalu", "ODC", 
   "GDP growth", 2019.`, 5.9374411336264`}, {"Tuvalu", "ODC", 
   "GDP growth", 2020.`, 7.00612804279335`}, {"Tuvalu", "ODC", 
   "GDP growth", 2021.`, 6.27479376033349`}, {"Tuvalu", "ODC", 
   "GDP shocks", 2002.`, -16.0091403253025`}, {"Tuvalu", "ODC", 
   "GDP shocks", 2003.`, -16.0091403253025`}, {"Tuvalu", "ODC", 
   "GDP shocks", 2004.`, -5.96132941753422`}, {"Tuvalu", "ODC", 
   "GDP shocks", 2005.`, -5.96132941753422`}, {"Tuvalu", "ODC", 
   "GDP shocks", 2009.`, -5.96132941753422`}, {"Tuvalu", "ODC", 
   "GDP shocks", 2010.`, -5.96132941753422`}, {"Tuvalu", "ODC", 
   "GDP shocks", 2015.`, -5.96132941753422`}, {"Tuvalu", "ODC", 
   "GDP shocks", 2016.`, -5.96132941753422`}, {"Tuvalu", "ODC", 
   "GDP shocks", 2017.`, -5.96132941753422`}, {"Tuvalu", "ODC", 
   "GDP shocks", 2018.`, -4.13246880255104`}, {"Tuvalu", "ODC", 
   "GDP shocks", 2019.`, -4.13246880255104`}};

You can see the above database in a table format:

jdata = Join@@Map[a \[Function] 
      Dataset[AssociationThread[First@a, #] & /@ Rest[a]]][{rawdata1}]
$\endgroup$
2
  • 1
    $\begingroup$ As formulated, yours is a request for work-for-hire, rather than a request for help. Any approach would necessarily have to be tailored to the structure of your data set, and therefore be of little general use to future readers. Instead, could you include what you have tried so far, and identify the specific step that gave you trouble? $\endgroup$
    – MarcoB
    Oct 26, 2021 at 16:09
  • $\begingroup$ @MarcoB : I tried various things to basically put the unformatted dataset into a formatted structure shown in the Link, which is the easiest way to get around my question/request. My trials not worth giving them here include various Matrix Operations on jdata using If and AppendTo statements. My purpose was to build a new matrix (i.e., a formatted dataset) of the selected elements. Once I construct it, then the rest of the answer is already given by @Bob Hanlon and@kglr. Any guidance or code to accomplish this transformation is sufficient for an answer. $\endgroup$ Oct 26, 2021 at 17:16

1 Answer 1

3
$\begingroup$
jdatagrouped = jdata[GroupBy[{#country &, #vars &}] /* KeySort,
   All, All, {DateObject[{Round@#time}], #data} &]

enter image description here

jdatagrouped["Bhutan", DateListPlot]

enter image description here

jdatagrouped[Map[DateListPlot]]["Bhutan"]

same picture

DateListPlot @ jdatagrouped["Bhutan"]

same picture

jdatagrouped[DateListPlot, "GDP growth"]

enter image description here

jdatagrouped[Map[DateListPlot], {"GDP growth", "GDP shocks"}]["Bangladesh"]

enter image description here

$\endgroup$
5
  • $\begingroup$ Using a different data set of the exact same structure above, the command jdatagrouped[Map[DateListPlot], {"Tax revenues"}]["Bhutan"] gives me the following error message: Failure[DateListPlot, Association["MessageTemplate" :> MessageName[DateListPlot, "dtvals"], "MessageParameters" -> {}]]["Bhutan"]. Furthermore, I re-wrote your code as jdatagrouped = jdata[GroupBy[{#country &, #status &, #vars &}] /* KeySort, All, All, {DateObject[{Round@#time}], #data} &]; Adding #status & produces an error. Any idea why? $\endgroup$ Oct 27, 2021 at 12:48
  • 1
    $\begingroup$ @TugrulTemel, not sure about the cause of first issue. The second issue can be fixed using {{#country , #status }&, #vars &} instead of {#country &, #status &, #vars &}. $\endgroup$
    – kglr
    Oct 27, 2021 at 13:30
  • 1
    $\begingroup$ It seems that normal commands for drawing are not suitable if I add #status. I tried to make a ListLinePlot of only those countries with LDC group or make a simple average of GDP growth only for the LDC group. Basically, my question is what is the order of selection of the parameters? $\endgroup$ Oct 27, 2021 at 19:34
  • $\begingroup$ " DateListPlot[jdatagrouped["Bhutan", {"GDP growth", "GDP shocks"}], Filling -> Bottom] is working. $\endgroup$ Oct 27, 2021 at 19:49
  • 1
    $\begingroup$ @TugrulTemel, try using groupedjd = jdata[GroupBy[{#status &, #country &, #vars &}], All, All, All, {DateObject[{Round@#time}], #data} &] . Then you can use DateListPlot@groupedjd["LDC", {"Bhutan", "Nepal"}, "GDP growth"] , DateListPlot /@ groupedjd["LDC", All, {"GDP growth", "GDP shocks"}] etc. $\endgroup$
    – kglr
    Oct 27, 2021 at 21:26

Your Answer

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

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