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\", \"
"Invalid .internal.selfref detected and fixed by taking a copy..."
No need to make a copy when assigning id2 within f2() you can add a column directly by altering:
# From:
x <- x[, id2 := which(vapply(groups, function(x,y) any(x==y), .BY[[1]], FUN.VALUE=T)), by=id]
# To something along the lines of:
x$id2 <- findInterval( match( x$id, unlist(groups)), cumsum(c(0,sapply(groups, length)))+1)
Then you can continue use your 'x' data.table like normal without incurring a warning.
Also, to simply suppress the warning you can use suppressWarnings() around the f2(x[["res"]]) call.
Even on small tables there can be substantial performance difference:
Performance Comparison:
Unit: milliseconds
expr min lq median uq max neval
f.main() 2.896716 2.982045 3.034334 3.137628 7.542367 100
suppressWarnings(f.main()) 3.005142 3.081811 3.133137 3.210126 5.363575 100
f.main.direct() 1.279303 1.384521 1.413713 1.486853 5.684363 100