好的所以我正在编写这个循环,我打算循环并将我的矢量名称粘贴到循环中。粘贴变量名称的目标目标是在URL链接中,以及在“”标记之间的destfile名称。
到目前为止,这是我的程序:
# 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")
}
正如你所看到的...我希望在向量中输入变量的名称并粘贴到我的字符串中以获取url和destfile ...有什么办法可以做到这一点吗?
现在它不会认识到我希望这个名字是一个变量,因为它已经在“”中标记它会把它作为它的书写。
答案 0 :(得分:2)
这似乎就是你所追求的。
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")
}