我有以下一段代码作为测试
library(data.table)
set.seed(1)
dt <- data.table::data.table(letters = sample(LETTERS,200,replace = TRUE),numbers = sample(1:1000,200,replace = TRUE))
sortcols <- c("letters","numbers")
setkeyv(dt,sortcols)
dt
# letters numbers
# 1: A 258
# 2: A 400
# 3: A 429
# 4: A 496
# 5: B 72
# ---
#196: Z 142
#197: Z 462
#198: Z 583
#199: Z 665
#200: Z 954
dt[letters=="A"][,numbers := numbers/2]
dt2 <- dt[letters=="A"][,numbers := numbers/2]
dt[letters=="A"]
# letters numbers
#1: A 258
#2: A 400
#3: A 429
#4: A 496
dt2
# letters numbers
#1: A 129.0
#2: A 200.0
#3: A 214.5
#4: A 248.0
我在这里想要了解的是
dt [letters ==“A”] [,numbers:= numbers / 2]
不影响基础数据表dt(并且所有字母的数字列保持相同==“A”)而当我将其复制为dt2时,它会反映在复制的结果中(即数字)对于所有字母==“A”,dt2中的列被除以2,这是代码的原始意图)。不是通过引用运算符的函数:=,来影响基础数据表而不进行复制吗?