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 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!

share|improve this question
add comment

1 Answer

up vote 7 down vote accepted

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.

share|improve this answer
    
+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 ;-) –  halirutan Aug 17 '12 at 3:23
    
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. –  Andrew Brēza Aug 17 '12 at 3:24
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.