Andrew Bannerman
Andrew Bannerman

Reputation: 1305

Insert variable within " " character string

Ok so I am writing this loop which I intend to loop through and paste the name from my vector into the loop. The intended destination for the paste of the variable name is to be within a URL link and also a destfile name between " " marks.

So far this is my procedure:

    # Download ETF AUM Files 
    file.list <- c("DDM","MVV","QLD","SAA","SSO","TQQQ","UDOW","UMDD","UPRO","URTY","UWM", "BIB", "FINU","LTL","ROM", "RXL", "SVXY","UBIO","UCC","UGE","UPW","URE","USD","UXI","UYG","UYM","DOG","DXD","MYY","MZZ","PSQ","QID","RWM","SBB","SDD","SDOW","SDS","SH","SPXU","SQQQ","SRTY","TWM","SMDD","UVXY","VIXM","VIXY")


    # Loop 
     for (f in 1:length(file.list)) {
        next.file <- file.list[i]
        file.name.variable <- paste(next.file[1])
        url <- "https://accounts.profunds.com/etfdata/ByFund/file.name.variable-historical_nav.csv"
        destfile <- "C:/R Projects/Data/etf_aum/file.name.variable.csv"
        download.file(url, destfile, mode="wb")
}

As you can see... I wish to enter the name of my variable inside the vector and paste into into my character strings for the url and destfile... Is there any way to do this?

Right now it wouldn't recognize that I wish the name to be a variable, as its already in " " marks it would take it as its written.

Upvotes: 1

Views: 11660

Answers (1)

kangaroo_cliff
kangaroo_cliff

Reputation: 6222

It seems this is what you are after.

for (i in 1 : length(file.list)) {
  file.name.variable <-  file.list[i]

  url <- paste0("https://accounts.profunds.com/etfdata/ByFund/", 
                   file.name.variable, "-historical_nav.csv")

  destfile <- paste0("C:/R Projects/Data/etf_aum/",
                          file.name.variable, "csv")

  download.file(url, destfile, mode="wb")
}

Upvotes: 4

Related Questions