Invalid .internal.selfref in data.table

后端 未结 3 765
半阙折子戏
半阙折子戏 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:34

    Yes, the problem is the list. Here is a simple example:

    DT <- data.table(1:5)
    mylist1 <- list(DT,"a")
    mylist1[[1]][,id:=.I]
    #warning
    
    mylist2 <- list(data.table(1:5),"a")
    mylist2[[1]][,id:=.I]
    #no warning
    

    You should avoid copying a data.table into a list (and to be on the safe side I would avoid having a DT in a list at all). Try this:

    f1 <- function(){
      mylist <- list(res=data.table(id = c("aaaa", "aaaa", "aaas", "aaas", "bbbb", "bbbb"),
                     period = c("start", "end", "start", "end", "start", "end"),
                     date = structure(c(15401L, 15401L, 15581L, 15762L, 15430L, 15747L), class = c("IDate", "Date"))))
      other_results <- ""
      mylist$other_results <- other_results
      mylist
    }
    

提交回复
热议问题