Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于API调用的循环,然后插入到R中的单个数据帧中_R_Api_Loops_Dataframe - Fatal编程技术网

用于API调用的循环,然后插入到R中的单个数据帧中

用于API调用的循环,然后插入到R中的单个数据帧中,r,api,loops,dataframe,R,Api,Loops,Dataframe,我希望从R中的多个JSON API调用创建一个大型数据集。当我运行代码时,它返回零错误,但它会生成一个名为all_prices的空数据帧。这是我正在使用的代码: library(jsonlite) library(data.table) library(anytime) library(xts) library(quantmod) response <- fromJSON('https://www.cryptocompare.com/api/data/coinlist') cryptoc

我希望从R中的多个JSON API调用创建一个大型数据集。当我运行代码时,它返回零错误,但它会生成一个名为all_prices的空数据帧。这是我正在使用的代码:

library(jsonlite)
library(data.table)
library(anytime)
library(xts)
library(quantmod)

response <- fromJSON('https://www.cryptocompare.com/api/data/coinlist')
cryptocurrencies <- data.table::rbindlist(response$Data, fill=TRUE)

for (i in 1:10) {

prices  <- fromJSON(paste0("https://min-api.cryptocompare.com/data/histoday?
fsym=",as.character(cryptocurrencies[1,5]),
"&tsym=USD&allData=true&e=CCCAGG"))
prices                   <- data.frame(prices$Data)
prices$Symbol            <- rep(cryptocurrencies[1,5],nrow(prices))

all_prices = do.call(rbind, prices)
}
我希望通过rbind将每个交互连接到数据帧,以创建一个数据集


我想问题在于do.call函数,不过我猜。谢谢

这应该可以做到:

all_prices <- data.frame()
for (i in 1:10) {
  prices <- fromJSON(paste0("https://min-api.cryptocompare.com/data/histoday?fsym=",
                            as.character(cryptocurrencies[i,5]),
                             "&tsym=USD&allData=true&e=CCCAGG"))$Data
  prices$Symbol            <- rep(cryptocurrencies[i,5],nrow(prices))
  all_prices <- rbind(all_prices, prices)
}
prices$Data已经是一个数据框架,正如您之前所做的那样,您将在每次迭代中过度编写所有的_价格。您也在使用i循环,但从未在for循环中的任何位置使用过它。

考虑使用lappy来构建数据帧列表,然后运行do.call甚至data。循环外的表的rbindlist:

df_list <- lapply(1:10, function(i) {
      json  <- fromJSON(paste0("https://min-api.cryptocompare.co/data/histoday?fsym=",
                               as.character(cryptocurrencies[i,5]),
                               "&tsym=USD&allData=true&e=CCCAGG"))

      prices <- data.frame(json$Data)
      prices$Symbol <- rep(cryptocurrencies[i,5], nrow(prices))

      return(prices)
})

all_prices <- base::do.call(rbind, df_list)

all_prices <- data.table::rbindlist(df_list)

如果有帮助的话,如果你接受了答案就太好了。谢谢