作为最低限度的演示,我想要将因子列y
从dt2
复制到dt1
:
library(data.table)
dt1 <- data.table(id = 1:2, x = 3:4, key = "id")
dt2 <- data.table(id = 2:3, y = factor(letters[1:2]), key = "id")
dt1[dt2, y := y] # do copy
class(dt1$y) # y ended up as integers in dt1
## "integer"
class(dt2$y) # what y should be
## "factor"
y
未被指定为因素。任何解决方案?