4
$\begingroup$

I have used WolframAlpha since it was launched (now a pro member) but am new to Mathematica 8. Is there a way to automatically add data from WA to a Mathematica dataset? For example, what if I have data on annual sales and I want to add the USA's GDP in the given year as the third column. I have used the WolframAlpha[] function but am not sure how to integrate it into my data. I'd want to add a "GDP" column to this data:

In[4]:= sales = Import["C:\\Users\\Andrew\\Desktop\\DemoData.xlsx"]

Out[4]= {{{"Year", "Sales"}, {2000., 105704.}, {2001., 
120042.}, {2002., 118973.}, {2003., 126539.}, {2004., 
105582.}, {2005., 126510.}, {2006., 101745.}, {2007., 
105821.}, {2008., 127313.}, {2009., 100908.}, {2010., 
115692.}, {2011., 116188.}}}

This isn't the best example, but this kind of thing would make my grad school life markedly easier. No more downloading, cleaning, and merging independent variable sets from random sources!

$\endgroup$

1 Answer 1

8
$\begingroup$

You can access historical GPD data via the WolframAlpha command like so:

gdpData = WolframAlpha["us gdp",
  {{"History:GrossDomesticProduct:EconomicData",1},"ComputableData"}];

This returns quarterly data going back to 1947 in a structured list. You can manipulate the data just as you would any data in Mathematica. Thus, you can get the data points at the first of the year in the date range desired like so:

gdpDataRestricted = First/@ SplitBy[Select[gdpData, 
  2000<=#[[1,1]]<=2011&],#[[1,1]]&];

You can then combine it with your data and display the result.

yourData = {{2000.,105704.},{2001.,120042.},{2002.,118973.},
  {2003.,126539.},{2004.,105582.},{2005.,126510.},{2006.,101745.},
  {2007.,105821.},{2008.,127313.},{2009.,100908.},{2010.,115692.},
  {2011.,116188.}};
allData = Table[Append[yourData[[k]],gdpDataRestricted[[k,-1]]],
  {k,1,Length[yourData]}];
Grid[Prepend[allData,{"Year","Sales", "GDP"}], Dividers -> All]

enter image description here

Figuring out the WolframAlpha command can be tricky. I usually start with the most basic command (in this case, WolframAlpha["us gdp"]) and then use the + signs in the upper right portion of the pods to find the correct invocation to get at the actual data.

$\endgroup$
2
  • $\begingroup$ +1 I was just about to edit your post and include a screenshot how one can extract the specific WolframAlpha call when you added it ;-) $\endgroup$
    – halirutan
    Commented Aug 17, 2012 at 3:23
  • $\begingroup$ Exactly what I wanted! You'd be surprised at how many researchers, professors, and TAs spend hours adding data by processing and combining data files. $\endgroup$ Commented Aug 17, 2012 at 3:24

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.