1

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.

1 Answer 1

4

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")
}
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome! So all I needed was to ",file.name.variable," inserted unto my " " strings -fantastic - thank you, learn something new every day + the paste0 to remove my sep""
paste is vectorized. So it would be more efficient to create the urls and destfiles outside the loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.