[R] SAS Macro Variable in R

Thomas Lumley tlumley at u.washington.edu
Tue Jun 23 22:02:25 CEST 2009


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




More information about the R-help mailing list