小编JRM*_*cia的帖子

为巨大的数据表元素赋值太慢

给定data.table定义如下:

n <- 34916339
ds2 <- data.table(X=rep(as.integer(NA),n), Y=rep(as.integer(NA),n), 
                  LAT=rep(as.numeric(NA),n), LON=rep(as.numeric(NA),n),
                  FCT_DATE=rep(as.Date(NA),n), VAR=rep(as.character(NA),n), 
                  TYPE=rep(as.character(NA),n), VALUE=rep(as.numeric(NA),n))
Run Code Online (Sandbox Code Playgroud)

以下代码太慢了.

ds = data.table dim = 572399 x 66
colNames <- rep("any_string",66)  # only an example

for (i in 1:nrow(ds)) {
   for (j in 6:66) {
       colName <- colNames[j]
       colName.split <- strsplit(colName, "_")    # Split the elements by "_"   

       k <- ((i-1) * length(colIndex))+(j-5)  # creates 61 lines each complete loop
       ds2[k,6] <- colName.split[[1]][1]      
       ds2[k,7] <- colName.split[[1]][2]      # so, it reads 61 cols from ds
       ds2[k,8] …
Run Code Online (Sandbox Code Playgroud)

performance r data.table

2
推荐指数
1
解决办法
170
查看次数

标签 统计

data.table ×1

performance ×1

r ×1