Hi I'm new to R and would like to implement a SAS-like macro variable in R. What I'd like to do is take the simple R code below and change the "=TEF" to different letters to refer to different companies' data for download. # DOWNLOADS FILES FROM YAHOO INTERNET download.file('http://ichart.yahoo.com/table.csv?s=TEF.MC&a=00&b=1&c=2003&d=05&e=23&f=2009&g=d&ignore=.csv', 'c:\\projects\\stock data\\data\\test.csv',quiet=TRUE) As you can see the text I want to change is within the quoted Internet address. Is this possible in R? Thanks in advance for any assistance. -- Best regards, David Young Marketing and Statistical Consultant Madrid, Spain +34 913 540 381 http://www.linkedin.com/in/europedavidyoung mailto:dyoung at telefonica.net
You can use sprintf: u <- sprintf(' http://ichart.yahoo.com/table.csv?s=%s.MC&a=00&b=1&c=2003&d=05&e=23&f=2009&g=d&ignore=.csv<http://ichart.yahoo.com/table.csv?s=TEF.MC&a=00&b=1&c=2003&d=05&e=23&f=2009&g=d&ignore=.csv>', "TEF") download.file(u, 'c:\\projects\\stock data\\data\\test.csv',quiet=TRUE) On Tue, Jun 23, 2009 at 2:40 PM, David Young <dyoung@telefonica.net> wrote:> Hi I'm new to R and would like to implement a SAS-like macro variable > in R. > > What I'd like to do is take the simple R code below and change the > "=TEF" to different letters to refer to different companies' data for > download. > > # DOWNLOADS FILES FROM YAHOO INTERNET > download.file(' > http://ichart.yahoo.com/table.csv?s=TEF.MC&a=00&b=1&c=2003&d=05&e=23&f=2009&g=d&ignore=.csv > ', > 'c:\\projects\\stock data\\data\\test.csv',quiet=TRUE) > > As you can see the text I want to change is within the quoted Internet > address. Is this possible in R? > > Thanks in advance for any assistance. > > > > -- > Best regards, > > David Young > Marketing and Statistical Consultant > Madrid, Spain > +34 913 540 381 > http://www.linkedin.com/in/europedavidyoung > > mailto:dyoung@telefonica.net > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
On Tuesday, June 23, 2009 1:40 PM, David Young wrote: > ...Hi I'm new to R and would like to > implement a SAS-like macro variable in R. > What I'd like to do is take the simple R > code below and change the "=TEF" to > different letters to refer to different > companies' data for download... > # DOWNLOADS FILES FROM YAHOO INTERNET > download.file('http://ichart.yahoo.com/table.csv?s=TEF.MC > &a=00&b=1&c=2003&d=05&e=23&f=2009&g=d&ignore=.csv', > 'c:\\projects\\stock data\\data\\test.csv',quiet=TRUE) > ...As you can see the text I want to change > is within the quoted Internet address. Is > this possible in R... How about something like: download.ichart <- function ( symbol , destpath = 'c:/projects/stock data/data/' ) { url <- paste ( "http://ichart.yahoo.com/table.csv?s=" , substitute ( symbol ) , ".MC&a=00&b=1&c=2003&d=05&e=23&f=2009&g=d&ignore=.csv" , sep = "" ) destfile <- paste ( destpath , substitute ( symbol ) , ".csv" , sep = "" ) download.file ( url , destfile , quiet = TRUE ) } download.ichart ( "TEF" ) -- David ? ----------------------------------------------------- David Huffer, Ph.D. Senior Statistician CSOSA/Washington, DC david.huffer at csosa.gov
On Tue, 23 Jun 2009, David Young wrote:> Hi I'm new to R and would like to implement a SAS-like macro variable > in R. > > What I'd like to do is take the simple R code below and change the > "=TEF" to different letters to refer to different companies' data for > download. > > # DOWNLOADS FILES FROM YAHOO INTERNET > download.file('http://ichart.yahoo.com/table.csv?s=TEF.MC&a=00&b=1&c=2003&d=05&e=23&f=2009&g=d&ignore=.csv', > 'c:\\projects\\stock data\\data\\test.csv',quiet=TRUE) > > As you can see the text I want to change is within the quoted Internet > address. Is this possible in R?I do this sort of thing with sub() or gsub() download.file(sub('TEF','http://ichart.yahoo.com/table.csv?s=TEF.MC&a=00&b=1&c=2003&d=05&e=23&f=2009&g=d&ignore=.csv',thing_to_sub_for_TEF), 'c:\\projects\\stock data\\data\\test.csv',quiet=TRUE) I would also use forward slashes / rather than double backslashes \\, but that's just a matter of taste. -thomas Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle
I typically do something like this: nm <- 'TEF' ## split into multiple lines only to make it easier to read dpath <- paste('http:// ichart.yahoo.com/table.csv?s=', nm, '.MC&a=00&b=1&c=2003&d=05&e=23&f=2009&g=d&ignore=.csv', sep='') download.file( dpath, 'c:\\projects\\stock data\\data\\test.csv',quiet=TRUE) Then just change the value of nm to different letters. I have also used the sub() or gsub() approach like Thomas, and the "wrap it in a function" approach like David, though I would use paste() inside the function, not the substitute() that he uses. So you see, R tends to have multiple ways to do things. -Don At 7:40 PM +0200 6/23/09, David Young wrote:>Hi I'm new to R and would like to implement a SAS-like macro variable >in R. > >What I'd like to do is take the simple R code below and change the >"=TEF" to different letters to refer to different companies' data for >download. > ># DOWNLOADS FILES FROM YAHOO INTERNET >download.file('http:// >ichart.yahoo.com/table.csv?s=TEF.MC&a=00&b=1&c=2003&d=05&e=23&f=2009&g=d&ignore=.csv', > 'c:\\projects\\stock data\\data\\test.csv',quiet=TRUE) > >As you can see the text I want to change is within the quoted Internet >address. Is this possible in R? > >Thanks in advance for any assistance. > > > >-- >Best regards, > >David Young >Marketing and Statistical Consultant >Madrid, Spain >+34 913 540 381 >http:// www. linkedin.com/in/europedavidyoung > > mailto:dyoung at telefonica.net > >______________________________________________ >R-help at 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.-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062