Skip to content

Merge update silently fails to coerce character to factor #2796

Closed

Description

@MichaelChirico
# use merge to define b; DT2$b is character
DT1 = data.table(a = 1:5)
ltr = LETTERS[1:5]
DT2 = data.table(a = 1:5, b = ltr)
DT1[DT2, b := i.b, on = 'a']
DT1$b
# [1] "A" "B" "C" "D" "E"

# use merge to update b; DT2$b is factor
DT2[ , b := factor(ltr)]
DT1[DT2, b := i.b, on = 'a']
DT2$b
# [1] A B C D E
# Levels: A B C D E
DT1$b
# [1] "A" "B" "C" "D" "E"

Not the case for changing b to integer (warns that we need explicit type conversion):

DT2[ , b := 5:1]
DT1[DT2, b := i.b, on = 'a']

Warning message: In [.data.table(DT1, DT2, :=(b, i.b), on = "a") : Coerced 'integer' RHS to 'character' to match the column's type. Either change the target column ['b'] to 'integer' first (by creating a new 'integer' vector length 5 (nrows of entire table) and assign that; i.e. 'replace' column), or coerce RHS to 'character' (e.g. 1L, NA_[real|integer]_, as.*, etc) to make your intent clear and for speed. Or, set the column type correctly up front when you create the table and stick to it, please.

This may be fundamentally the same issue as #2403. Either we should be warned about the need for explicit type casting, or the factor conversion should work; here I wasn't thinking beforehand about any need for type conversion since all rows are being updated.

Activity

  1. MichaelChirico commented on May 3, 2018

    @MichaelChirico
    MemberAuthor

    The same problem occurs when updating from c('ordered', 'factor') to plain 'factor' -- class from x is retained silently

  2. franknarf1 commented on May 3, 2018

    @franknarf1
    Contributor

    Not a fix, but a workaround...

    library(data.table)
    ltr = LETTERS[1:5]
    DT1 = data.table(a = 1:5, b = ltr)
    DT2 = data.table(a = 1:5, b = factor(ltr, levels = ltr))
    
    DT1[, b := DT2[.SD, on=.(a), x.b]]
    

    I guess the problem is that := tries to retain column attributes apart from storage.mode when i is nonmissing ... and arguably it should do so ...? So maybe warn whenever i is present and := assigns to an existing non-atomic column, along the lines of

    Sub-assigning to columns b, c, d in table DT1 while retaining attributes. If you want to overwrite column attributes, assign to the full column without using i to select rows.

    Maybe there could be some option to error out in that case instead so you're forced to be clearer about the operation.

  3. MichaelChirico commented on May 4, 2018

    @MichaelChirico
    MemberAuthor

    Yea, IMO this should be a warning, as it would be if we tried to update from character to numeric, e.g.

  4. MichaelChirico commented on May 23, 2019

    @MichaelChirico
    MemberAuthor

    This is now reported in verbose output:

    DT1[DT2, b := i.b, on = 'a', verbose = TRUE]
    i. a has same type ( integer ) as x. a . No coercion needed.Calculated ad hoc index in 0.000s elapsed (0.000s cpu) 
    Starting bmerge ...done in 0.000s elapsed (0.000s cpu) 
    Constructing irows for '!byjoin || nqbyjoin' ... 0.000s elapsed (0.000s cpu) 
    Detected that j uses these columns: b,i.b 
    Assigning to 5 row subset of 5 rows
    RHS_list_of_columns == false
    Coerced factor RHS to character to match the column's type. Avoid this coercion if possible, for efficiency, by creating RHS as type character.
    

    Closed I assume by #2734

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions