Invalid .internal.selfref in data.table

后端 未结 3 770
半阙折子戏
半阙折子戏 2020-12-31 06:59

I needed to assign a \"second\" id to group some values inside my original id. this is my sample data:

dt<-structure(list(id = c(\"aaaa\", \"         


        
3条回答
  •  臣服心动
    2020-12-31 07:39

    You could "shallow copy" while creating the list, so that 1) you don't do full memory copy (speed isn't affected) and 2) you don't get internal ref error (thanks to @mnel for this trick).

    Creating data:

    set.seed(45)
    ss <- function() {
        tt <- sample(1:10, 1e6, replace=TRUE)
    }
    tt <- replicate(100, ss(), simplify=FALSE)
    tt <- as.data.table(tt)
    

    How you should go about creating the list (shallow copy):

    system.time( {
        ll <- list(d1 = { # shallow copy here...
            data.table:::settruelength(tt, 0)
            invisible(alloc.col(tt))
        }, "a")
    })
    user  system elapsed
       0       0       0
    > system.time(tt[, bla := 2])
       user  system elapsed
      0.012   0.000   0.013
    > system.time(ll[[1]][, bla :=2 ])
       user  system elapsed
      0.008   0.000   0.010
    

    So you don't compromise in speed and you don't get a warning followed by a full copy. Hope this helps.

提交回复
热议问题