mchen
mchen

Reputation: 10166

Cannot assign factors by reference in data.table

As a minimal demonstration, say I want to copy factor column y from dt2 to 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 is not getting assigned as a factor. Any solutions?

Upvotes: 0

Views: 188

Answers (1)

mchen
mchen

Reputation: 10166

To paraphrase Arun:

  • Bug has been fixed in v1.9.3
  • If you get CsubsetVector errors when installing from github, see issue #757

Upvotes: 1

Related Questions