You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Merge update silently fails to coerce character to factor #2796
# 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.
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.
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.
Activity
MichaelChirico commentedon May 3, 2018
The same problem occurs when updating from
c('ordered', 'factor')to plain'factor'-- class fromxis retained silentlyfranknarf1 commentedon May 3, 2018
Not a fix, but a workaround...
I guess the problem is that
:=tries to retain column attributes apart from storage.mode wheniis nonmissing ... and arguably it should do so ...? So maybe warn wheneveriis present and:=assigns to an existing non-atomic column, along the lines ofMaybe there could be some option to error out in that case instead so you're forced to be clearer about the operation.
MichaelChirico commentedon May 4, 2018
Yea, IMO this should be a warning, as it would be if we tried to update from
charactertonumeric, e.g.MichaelChirico commentedon May 23, 2019
This is now reported in
verboseoutput:Closed I assume by #2734