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
Allow 0 and 1 singletons to coerce to logical without warning in set() and := ? #996
DT= data.table(a=1:4, b=c(FALSE, TRUE, NA, FALSE))
DT# a b#1: 1 FALSE#2: 2 TRUE#3: 3 NA#4: 4 FALSE
set(DT,3L,1L,0) # ok, no warning
set(DT,3L,2L,0) # should be ok too without warning# Warning message:# In set(DT, 3L, 2L, 0) :# Coerced 'double' RHS to 'logical' to match the column's type; may have truncated precision. Either # change the target column to 'double' first (by creating a new 'double' vector length 4 (nrows of entire # table) and assign that; i.e. 'replace' column), or coerce RHS to 'logical' (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.DT# a b#1: 1 FALSE#2: 2 TRUE#3: 0 FALSE#4: 4 FALSE
Apply to 0, 1, 0L and 1L. Just length-1 vectors (i.e. singletons), not longer, as already in place for 0 and 1 to integer.
changed the title [-]Allow 0 and 1 singletons to coerce to logical without warning in `set()` and `:=`[/-][+]Allow 0 and 1 singletons to coerce to logical without warning in `set()` and `:=` ?[/+]on Jan 5, 2015
Maybe leave the warning in place for speed (we want to be warned about unnecessary repetitive coercing). Or just leave the warning in place for set() (whose purpose is to be iterated) and drop just for :=.
I prefer to leave the warning, because usually (for me at least) this warning is only produced when I've been sloppy & tells me I need to go back and use 0L, F, etc. as appropriate.
I would be fine with an option to trigger this, of course.
Warning message:
In `[.data.table`(DT1, code == "c", `:=`(valB, valB + 1)) :
Coerced double RHS to integer to match the type of the target column (column 4 named 'valB'). The RHS values contain no fractions so would be more efficiently created as integer. Consider using R's 'L' postfix (typeof(0L) vs typeof(0)) to create constants as integer and avoid this warning. Wrapping the RHS with as.integer() will avoid this warning too but it's better if possible to create the RHS as integer in the first place so that the cost of the coercion can be avoided.
Activity
[-]Allow 0 and 1 singletons to coerce to logical without warning in `set()` and `:=`[/-][+]Allow 0 and 1 singletons to coerce to logical without warning in `set()` and `:=` ?[/+]mattdowle commentedon Jan 5, 2015
Maybe leave the warning in place for speed (we want to be warned about unnecessary repetitive coercing). Or just leave the warning in place for
set()(whose purpose is to be iterated) and drop just for:=.MichaelChirico commentedon Jul 27, 2015
I prefer to leave the warning, because usually (for me at least) this warning is only produced when I've been sloppy & tells me I need to go back and use
0L,F, etc. as appropriate.I would be fine with an
optionto trigger this, of course.MichaelChirico commentedon May 23, 2019
Has our experience with
logical01infread/fwriteinformed this issue at all?jangorecki commentedon May 24, 2019
IMO because
setis meant to be low overhead it is best to avoid coercion and leave the warningjangorecki commentedon Sep 20, 2019
on the other hand
:=should be more relaxed and not raise warnings in cases like