Why is data.table copied when returning from a map

I realized that data.table not copied when returning from a function. However, in this particular case, it is copied. Can anyone explain why?

 dt1 <- data.table(a=1) dt2 <- data.table(b=1) dt3 <- data.table(c=1) address(dt1); address(dt2); address(dt3) [1] "000000005E20D990" [1] "00000000052301E8" [1] "000000001D622210" l <- list(a=dt1, b=dt2, c=dt3) address(l$a); address(l$b); address(l$c) $[1] "000000005E20D990" $[1] "00000000052301E8" $[1] "000000001D622210" f <- function(dt) {setnames(dt, toupper(names(dt)))} l <- Map(f, l) address(l$a); address(l$b); address(l$c) $[1] "000000001945C7B0" $[1] "0000000066858738" $[1] "000000001B021038" dt1 $ A $1: 1 dt2 $ B $1: 1 dt3 $ C $1: 1 

So this is the last line that makes a copy. However, the following does not create a copy.

 address(dt1) $[1] "000000005E20D990" dt4 <- f(dt1) address(dt4) $[1] "000000005E20D990" 

What am I missing?

Update As everyone noted, map or mapply makes a copy. lapply works in the above case, but my actual code needs multiple function inputs. I realized that all apply functions use the same code. But this does not seem to be the case.

+5
source share

Source: https://habr.com/ru/post/1241128/


All Articles