Skip to content

Error in update join when joining on factor #3559

@Henrik-P

Description

@Henrik-P

I want to do an update join where I replace values in one data.table with values from another. For example, replace NA in 'd1' with values from 'd2':

d1 <- data.table(fac = factor(letters[1:4]), char = letters[1:4], val = c(1L, NA, 3L, NA))
d2 <- data.table(fac = factor(letters[1:4]), char = letters[1:4], val = 1:4)

When joining on a factor variable 'fac', it errors:

d1[is.na(val), val := d2[.SD, x.val, on = .(fac)]]
# Error in set(i, j = lc, value = newfactor) : 
# .SD is locked. Updating .SD by reference using := or set are reserved for future use. 
# Use := in j directly. Or use copy(.SD) as a (slow) last resort, until shallow() is exported.

Same operation but joining on a character 'char' works:

d1[is.na(val), val := d2[.SD, x.val, on = .(char)]]
d1
#     fac char val
# 1:    a   a    1  
# 2:    b   b    2
# 3:    c   c    3
# 4:    d   d    4 

Searched among the issues for the error and found .SD locked error using foverlaps after ordering by variable (exactly the same error) and .SD is locked for DT[, DT2[.SD]] joins (nearly the same error). The latter indeed looks similar, but it doesn't seem to be directly related to the factor vs character issue described here.


data.table_1.12.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions