Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a large list of values which – for presentation in my LaTeX document – I need to format. For instance: I have calculated growth rates which I now would like to have formatted in the form of:

   In[2260]:= Map[{First[First[#]],NumberForm[N[Last[#]],3,2}]}&,
                CompleteQuarterlyStatDataBySubjects[[1]][[1]]]

    Out[2260]= {{Australia,{}},{Austria,-1.21},{Belgium,-1.46},{Canada,-1.30},
       {Czech Republic,-1.67},{Denmark,-1.87},{Estonia,-3.97},{Finland,-2.53},
       {France,-1.00},{Germany,-1.74},{Greece,-1.16},{Hungary,-1.45},
       {Iceland,-1.83}, {Ireland,-1.67},{Israel,{}},{Italy,-1.42},{Japan,-0.69},
       {Korea, {}},{Luxembourg,-1.67},{Netherlands,-0.98},{New Zealand,-0.50},
       {Norway,-0.64},{Poland,{}},{Portugal,-0.40},{Slovak Republic,{}},
       {Slovenia,-3.31},{Spain,-0.71},{Sweden,{}},{Switzerland,-0.82},
       {Turkey,-3.39},{United Kingdom,-1.45},{United States,-1.28}}

(the original dataset CompleteQuarterlyStatDataBySubjects[[1]][[1]] is at the bottom of this post)

Besides growth rates I have calculated AbsoluteTime-Values which I would like to format as follows:

In[2271]:= Map[
             {
              First[First[#]],
              Table[
                DateString[#[[2;;-1]][[i]],{"Quarter"," ","Year"}],
                {i,Length[#[[2;;-1]]]}
              ]
             }&,
             CompleteQuarterlyStatDataBySubjects[[1]][[5]]
           ]

Out[2271]= {{Australia,{DateString[{},{Quarter, ,Year}]}},{Austria,{1 2008}},
            {Belgium,{2 2008}},{Canada,{3 2008}},{Czech Republic,{3 2008}},
            {Denmark,{2 2008}},{Estonia,{2 2008}},{Finland,{2 2008}},
            {France,{1 2008}},{Germany,{1 2008}},{Greece,{3 2008}},
            {Hungary,{1 2008}},{Iceland,{3 2008}},{Ireland,{4 2007}},
            {Israel,{DateString[{},{Quarter, ,Year}]}},{Italy,{1 2008}},
            {Japan,{1 2008,3 2010}},{Korea,{DateString[{},{Quarter, ,Year}]}},
            {Luxembourg,{1 2008}},{Netherlands,{1 2008}},{New Zealand,{4 2007}},
            {Norway,{2 2008}},{Poland,{DateString[{},{Quarter, ,Year}]}},
            {Portugal,{4 2007,3 2010}},{Slovak Republic,{DateString[{},
            {Quarter, ,Year}]}},{Slovenia,{3 2008}},{Spain,{1 2008}},
            {Sweden,{DateString[{},{Quarter, ,Year}]}},{Switzerland,{2 2008}},
            {Turkey,{1 2008}},{United Kingdom,{1 2008}},{United States,{2 2008}}}

To automate the process of getting the values into the right format, I thought to use the following approach:

In[2261]:= Formats[list_,x_]:=
    If[x==1,
    NumberForm[N[list],{3,2}]
       If[x==5,
       Table[DateString[list[[i]],{"Quarter"," ","Year"}],{i,Length[list]}]
       ]
    ]

And then to type for example:

    Map[
      {
        First[First[#]],
        Formats[Last[#],1]
      }&,
      CompleteQuarterlyStatDataBySubjects[[1]][[1]]
    ]

Unfortunately that did not work! Does anyone has an idea how I can program a procedure to get my data automatically formatted?

The original data is:

 In[2259]:= CompleteQuarterlyStatDataBySubjects[[1]][[1]]
    Out[2259]= {{{Australia,AUS,other countries,GDP,quarterly data},{}},{{Austria,AUT,western European
    countries,GDP,quarterly data},-1.2056},{{Belgium,BEL,western European
    countries,GDP,quarterly data},-(7288960271654/4985881289361)},{{Canada,CAN,G7,GDP,quarterly data},-1.30053},{{Czech Republic,CZE,eastern European
    countries,GDP,quarterly data},-(2134917223477606900/1282008490172233089)},{{Denmark,DNK,nothern European
    countries,GDP,quarterly data},-(48057014595630800665/25700334274612164804)},{{Estonia,EST,eastern European
    countries,GDP,quarterly data},-3.9659},{{Finland,FIN,nothern European
    countries,GDP,quarterly data},-(33199059446612625/13128459048331036)},{{France,FRA,G7,GDP,quarterly data},-(5130109517723890585/5148852459134416857)},{{Germany,DEU,G7,GDP,quarterly data},-1.73904},{{Greece,GRC,southern European
    countries,GDP,quarterly data},-1.164},{{Hungary,HUN,eastern European
    countries,GDP,quarterly data},-(2225003087375671762980417961289391847/1539154631936274189423508152574973424)},{{Iceland,ISL,nothern European
    countries,GDP,quarterly data},-1.83398},{{Ireland,IRL,nothern European
    countries,GDP,quarterly data},-1.67021},{{Israel,ISR,other countries,GDP,quarterly data},{}},{{Italy,ITA,G7,GDP,quarterly data},-1.42009},{{Japan,JPN,G7,GDP,quarterly data},-(673269612770377724744620/262544695052599501095431),-(32760683814040103590/47201698472568566589)},{{Korea,KOR,other countries,GDP,quarterly data},{}},{{Luxembourg,LUX,western European
    countries,GDP,quarterly data},-1.67073},{{Netherlands,NLD,western European
    countries,GDP,quarterly data},-0.97505},{{New Zealand,NZL,other countries,GDP,quarterly data},-(152151059187768664247/302333262014074402254)},{{Norway,NOR,nothern European
    countries,GDP,quarterly data},-(33596749386427335752225/52657796909983176600514)},{{Poland,POL,eastern European
    countries,GDP,quarterly data},{}},{{Portugal,PRT,southern European
    countries,GDP,quarterly data},-0.785963,-0.398132},{{Slovak Republic,SVK,eastern European
    countries,GDP,quarterly data},{}},{{Slovenia,SVN,eastern European
    countries,GDP,quarterly data},-3.31102},{{Spain,ESP,southern European
    countries,GDP,quarterly data},-(2414923435622638997136189344226575/3392598745831412249336953513454508)},{{Sweden,SWE,nothern European
    countries,GDP,quarterly data},{}},{{Switzerland,CHE,western European
    countries,GDP,quarterly data},-0.820439},{{Turkey,TUR,other countries,GDP,quarterly data},-3.38638},{{United Kingdom,GBR,G7,GDP,quarterly data},-(2047013335956949212233044315/1411710881613564347284381614)},{{United States,USA,G7,GDP,quarterly data},-(2091354065961931475/1636350606517650408)}}

and for the AbsoluteTime-Values:

In[2265]:= CompleteQuarterlyStatDataBySubjects[[1]][[5]]
Out[2265]= {{{Australia,AUS,other countries,GDP,quarterly data},{}},{{Austria,AUT,western European
countries,GDP,quarterly data},3408134400},{{Belgium,BEL,western European
countries,GDP,quarterly data},3415996800},{{Canada,CAN,G7,GDP,quarterly data},3423859200},{{Czech Republic,CZE,eastern European
countries,GDP,quarterly data},3423859200},{{Denmark,DNK,nothern European
countries,GDP,quarterly data},3415996800},{{Estonia,EST,eastern European
countries,GDP,quarterly data},3415996800},{{Finland,FIN,nothern European
countries,GDP,quarterly data},3415996800},{{France,FRA,G7,GDP,quarterly data},3408134400},{{Germany,DEU,G7,GDP,quarterly data},3408134400},{{Greece,GRC,southern European
countries,GDP,quarterly data},3423859200},{{Hungary,HUN,eastern European
countries,GDP,quarterly data},3408134400},{{Iceland,ISL,nothern European
countries,GDP,quarterly data},3423859200},{{Ireland,IRL,nothern European
countries,GDP,quarterly data},3400185600},{{Israel,ISR,other countries,GDP,quarterly data},{}},{{Italy,ITA,G7,GDP,quarterly data},3408134400},{{Japan,JPN,G7,GDP,quarterly data},3408134400,3486931200},{{Korea,KOR,other countries,GDP,quarterly data},{}},{{Luxembourg,LUX,western European
countries,GDP,quarterly data},3408134400},{{Netherlands,NLD,western European
countries,GDP,quarterly data},3408134400},{{New Zealand,NZL,other countries,GDP,quarterly data},3400185600},{{Norway,NOR,nothern European
countries,GDP,quarterly data},3415996800},{{Poland,POL,eastern European
countries,GDP,quarterly data},{}},{{Portugal,PRT,southern European
countries,GDP,quarterly data},3400185600,3486931200},{{Slovak Republic,SVK,eastern European
countries,GDP,quarterly data},{}},{{Slovenia,SVN,eastern European
countries,GDP,quarterly data},3423859200},{{Spain,ESP,southern European
countries,GDP,quarterly data},3408134400},{{Sweden,SWE,nothern European
countries,GDP,quarterly data},{}},{{Switzerland,CHE,western European
countries,GDP,quarterly data},3415996800},{{Turkey,TUR,other countries,GDP,quarterly data},3408134400},{{United Kingdom,GBR,G7,GDP,quarterly data},3408134400},{{United States,USA,G7,GDP,quarterly data},3415996800}}
share|improve this question
    
It is not clear what output you expect. Do you desire a table with three columns: the country name, the growth rate, and the date? –  Mr.Wizard Feb 14 '12 at 15:34
    
I would like to have two tables each with two columns: country name & growth and country name & date. –  John Feb 14 '12 at 16:23
    
Glad to see you made it over here. Don't forget to check out meta for important discussions on how things work over here. In particular, here's a summary of some of them. –  rcollyer Feb 14 '12 at 18:09
add comment

1 Answer 1

(edit: you should use lower-case letters to start user function names so as not to conflict with built-ins.)

Please tell me if this does what you want:

formats[{}, _] = {};
formats[list_, 1] := NumberForm[N[list], {3, 2}]
formats[list_, 5] := DateString[list, {"Quarter", " ", "Year"}]

Testing. With dat1 set to your Out[2259]=, and dat2 set to your Out[2265]= :

Map[{First[First[#]], formats[Last[#], 1]} &, dat1]

{{Australia,{}},{Austria,-1.21},{Belgium,-1.46},{Canada,-1.30},{Czech Republic,-1.67},{Denmark,-1.87},{Estonia,-3.97},{Finland,-2.53},{France,-1.00},{Germany,-1.74},{Greece,-1.16},{Hungary,-1.45},{Iceland,-1.83},{Ireland,-1.67},{Israel,{}},{Italy,-1.42},{Japan,-0.69},{Korea,{}},{Luxembourg,-1.67},{Netherlands,-0.98},{New Zealand,-0.50},{Norway,-0.64},{Poland,{}},{Portugal,-0.40},{Republic Slovak,{}},{Slovenia,-3.31},{Spain,-0.71},{Sweden,{}},{Switzerland,-0.82},{Turkey,-3.39},{Kingdom United,-1.45},{States United,-1.28}}

Map[{First[First[#]], formats[Last[#], 5]} &, dat2]

{{Australia, {}}, {Austria, "1 2008"}, {Belgium, "2 2008"}, {Canada,
"3 2008"}, {Czech Republic, "3 2008"}, {Denmark, "2 2008"}, {Estonia, "2 2008"}, {Finland, "2 2008"}, {France, "1 2008"}, {Germany, "1 2008"}, {Greece, "3 2008"}, {Hungary, "1 2008"}, {Iceland, "3 2008"}, {Ireland, "4 2007"}, {Israel, {}}, {Italy, "1 2008"}, {Japan, "3 2010"}, {Korea, {}}, {Luxembourg, "1 2008"}, {Netherlands, "1 2008"}, {New Zealand, "4 2007"}, {Norway, "2 2008"}, {Poland, {}}, {Portugal, "3 2010"}, {Republic Slovak, {}}, {Slovenia, "3 2008"}, {Spain, "1 2008"}, {Sweden, {}}, {Switzerland, "2 2008"}, {Turkey, "1 2008"}, {Kingdom United, "1 2008"}, {States United, "2 2008"}}

share|improve this answer
    
Ups. Missed that one, but this even though, it does not work. –  John Feb 14 '12 at 15:28
    
@John please, what output are you expecting? I have trouble understanding your intention. –  Mr.Wizard Feb 14 '12 at 15:31
    
The output should be as in Output[2260] and Output[2271]. Perhaps further explanation helps to understand: As explained I have to cope with a large list of values, namely CompleteQuarterlyStatDataBySubjects[[j]][[i]]. As I am doing an econometric analysis this list contains 24 economic indicators (such as GDP and imports figures), whereas j stands for the indicators. In addition, for each indicator I have calculated 9 key figures (such as growth rates), represented by i. Each of this key figures need to be formatted in a different way as indicated in In[2260] and In[2271]. –  John Feb 14 '12 at 16:20
    
@John I have updated my answer. I also included the output that I am seeing. I hope that this does what you want. –  Mr.Wizard Feb 14 '12 at 16:44
1  
@John please note that because your own data has the country names as Symbols, rather than Strings, there are things such as States United appearing in my output; I expect that this will not happen with your original data. –  Mr.Wizard Feb 14 '12 at 16:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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