[R] Help with R loop for URL download from FRED to create US time series

2013-06-10 Thread arum
I am downloading time series data from FRED. I have a working download, but I
do not want to write out the download for all 50 states likes this:
 IDRGSP -
read.table('http://research.stlouisfed.org/fred2/data/IDRGSP.txt', skip=11,
header=TRUE)
IDRGSP$DATE - as.Date(IDRGSP$DATE, '%Y-%m-%d')
IDRGSP$SERIES - 'IDRGSP'
IDRGSP$DESC -  Real Total Gross Domestic Product by State for Idaho, Mil.
of, A, NSA, 2012-06-05

WYRGSP - read.table('http://research.stlouisfed.org/fred2/data/WYRGSP.txt',
skip=11, header=TRUE)
WYRGSP$DATE - as.Date(WYRGSP$DATE, '%Y-%m-%d')
WYRGSP$SERIES - 'WYRGSP'
WYRGSP$DESC -  Real Total Gross Domestic Product by State for Wyoming,
Mil. of, A, NSA, 2012-06-05
RGSP - rbind(IDRGSP, WYRGSP)

I want to loop but I can not get the paste to work correctly. I am trying
this:  Can someone help me figure out the loop so I can build a table for
all 50 states.
ab - c(state.abb)
base - 'http://research.stlouisfed.org/fred2/data/;
type - RGSP.txt', skip=11, header=TRUE;

tmp - NULL;
for (a in ab) {
  url - paste(base, a, type, sep=);

if (is.null(tmp))
tmp - read.table(url)
  else tmp - rbind(tmp, read.table(url))
}
tmp

thanks for your help




--
View this message in context: 
http://r.789695.n4.nabble.com/Help-with-R-loop-for-URL-download-from-FRED-to-create-US-time-series-tp4669209.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Help with R loop for URL download from FRED to create US time series

2013-06-10 Thread jim holtman
This should do it for you:


 base - http://research.stlouisfed.org/fred2/data/;

 files - lapply(state.abb, function(.state){
+ cat(.state, \n)
+ input - read.table(paste0(base, .state, RGSP.txt)
+ , skip = 11
+ , header = TRUE
+ , as.is = TRUE
+ )
+ input$DATE - as.Date(input$DATE, %Y-%m-%d)
+ input$SERIES - paste0(.state, RGSP)
+ input
+ })
AL
AK
AZ
AR
CA
CO
CT
DE
FL
GA
HI
ID
IL
IN
IA
KS
KY
LA
ME
MD
MA
MI
MN
MS
MO
MT
NE
NV
NH
NJ
NM
NY
NC
ND
OH
OK
OR
PA
RI
SC
SD
TN
TX
UT
VT
VA
WA
WV
WI
WY

 result - do.call(rbind, files)


 str(result)
'data.frame':   750 obs. of  3 variables:
 $ DATE  : Date, format: 1997-01-01 1998-01-01 1999-01-01
2000-01-01 ...
 $ VALUE : int  122541 126309 130898 132699 133888 137086 140020 146937
150968 153681 ...
 $ SERIES: chr  ALRGSP ALRGSP ALRGSP ALRGSP ...
 head(result,30)
 DATE  VALUE SERIES
1  1997-01-01 122541 ALRGSP
2  1998-01-01 126309 ALRGSP
3  1999-01-01 130898 ALRGSP
4  2000-01-01 132699 ALRGSP
5  2001-01-01 133888 ALRGSP
6  2002-01-01 137086 ALRGSP
7  2003-01-01 140020 ALRGSP
8  2004-01-01 146937 ALRGSP
9  2005-01-01 150968 ALRGSP
10 2006-01-01 153681 ALRGSP
11 2007-01-01 155388 ALRGSP
12 2008-01-01 155870 ALRGSP
13 2009-01-01 148074 ALRGSP
14 2010-01-01 151480 ALRGSP
15 2011-01-01 150330 ALRGSP
16 1997-01-01  37249 AKRGSP
17 1998-01-01  35341 AKRGSP
18 1999-01-01  34967 AKRGSP
19 2000-01-01  34192 AKRGSP
20 2001-01-01  35729 AKRGSP
21 2002-01-01  37111 AKRGSP
22 2003-01-01  36288 AKRGSP
23 2004-01-01  38179 AKRGSP
24 2005-01-01  37774 AKRGSP
25 2006-01-01  39836 AKRGSP
26 2007-01-01  40694 AKRGSP
27 2008-01-01  41039 AKRGSP
28 2009-01-01  44030 AKRGSP
29 2010-01-01  43591 AKRGSP
30 2011-01-01  44702 AKRGSP




On Mon, Jun 10, 2013 at 7:42 PM, arum arumk...@wrdf.org wrote:

 I am downloading time series data from FRED. I have a working download,
 but I
 do not want to write out the download for all 50 states likes this:
  IDRGSP -
 read.table('http://research.stlouisfed.org/fred2/data/IDRGSP.txt',
 skip=11,
 header=TRUE)
 IDRGSP$DATE - as.Date(IDRGSP$DATE, '%Y-%m-%d')
 IDRGSP$SERIES - 'IDRGSP'
 IDRGSP$DESC -  Real Total Gross Domestic Product by State for Idaho, Mil.
 of, A, NSA, 2012-06-05

 WYRGSP - read.table('http://research.stlouisfed.org/fred2/data/WYRGSP.txt
 ',
 skip=11, header=TRUE)
 WYRGSP$DATE - as.Date(WYRGSP$DATE, '%Y-%m-%d')
 WYRGSP$SERIES - 'WYRGSP'
 WYRGSP$DESC -  Real Total Gross Domestic Product by State for Wyoming,
 Mil. of, A, NSA, 2012-06-05
 RGSP - rbind(IDRGSP, WYRGSP)

 I want to loop but I can not get the paste to work correctly. I am trying
 this:  Can someone help me figure out the loop so I can build a table for
 all 50 states.
 ab - c(state.abb)
 base - 'http://research.stlouisfed.org/fred2/data/;
 type - RGSP.txt', skip=11, header=TRUE;

 tmp - NULL;
 for (a in ab) {
   url - paste(base, a, type, sep=);

 if (is.null(tmp))
 tmp - read.table(url)
   else tmp - rbind(tmp, read.table(url))
 }
 tmp

 thanks for your help




 --
 View this message in context:
 http://r.789695.n4.nabble.com/Help-with-R-loop-for-URL-download-from-FRED-to-create-US-time-series-tp4669209.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.




-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.