R cbind非常慢

问题描述 投票:1回答:1

有一个问题的标题非常相似(cbind运行非常缓慢)但它不能帮助我解决我的问题。我正在检索10多个JSON文件,每个文件有100个变量,我试图创建一个有1000列的大数据.frametable。在实践中,我并没有像例子中那样使用相同的JSON文件,而是使用不同的。理想情况下,只有问题行 cx <- cbind(cx, bx) 会加快速度,因为其他行(unlist,as.data.table)对我来说很好,我不知道还能用什么。我知道,"cbind很慢",但我有其他选择吗?最好是用Base R。

library(jsonlite)
library(data.table)

starttime <- Sys.time()
for (i in 1:10) {          # loop through all  10 json files
  zz <- Sys.time()         # measuring the time for each loop
  urlx <- "http://mysafeinfo.com/api/data?list=englishmonarchs&format=json"
  jsnx <- fromJSON(urlx)
  if(i==1) {
    ax <- unlist(jsnx)
    bx <- as.data.table(ax)
    cx <- bx
  }
  for (j in 1:100) {        # loop through all 100 variables in each file
    ax <- unlist(jsnx)
    bx <- as.data.table(ax)
    cx <- cbind(cx, bx) # <---- VERY SLOW ----
  }
  zz <- round(Sys.time()-zz,1)
  print(sprintf("%1.1f", zz))
  flush.console()
}
endtime  <- Sys.time()
endtime-starttime

这样文件越多越慢,这是我的计时。

[1] "0.7"
[1] "1.3"
[1] "1.3"
[1] "1.6"
[1] "2.1"
[1] "2.2"
[1] "2.5"
[1] "3.2"
[1] "3.4"
[1] "3.5"
r performance cbind
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.