Skip to content

Allow 0 and 1 singletons to coerce to logical without warning in set() and := ? #996

Description

@mattdowle

Highlighted via http://stackoverflow.com/questions/7235657/fastest-way-to-replace-nas-in-a-large-data-table/7249454?noredirect=1#comment43831879_7249454

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.

Activity

  1. added this to the v1.9.6 milestone on Jan 5, 2015
  2. 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
  3. mattdowle commented on Jan 5, 2015

    @mattdowle
    MemberAuthor

    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 :=.

  4. removed this from the v1.9.6 milestone on Feb 7, 2015
  5. MichaelChirico commented on Jul 27, 2015

    @MichaelChirico
    Member

    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.

  6. MichaelChirico commented on May 23, 2019

    @MichaelChirico
    Member

    Has our experience with logical01 in fread/fwrite informed this issue at all?

  7. jangorecki commented on May 24, 2019

    @jangorecki
    Member

    IMO because set is meant to be low overhead it is best to avoid coercion and leave the warning

  8. jangorecki commented on Sep 20, 2019

    @jangorecki
    Member

    on the other hand := should be more relaxed and not raise warnings in cases like

    DT1 = data.table(id = c(rep(1L,4L), 2L, 2L, 1L, 2L, 1L),
                   code = c("c","b","c","c","a","a","b","a","c"),
                   valA = 1:9, valB = 10:18,
                   stringsAsFactors = FALSE)
    DT1[code=="c", valB := valB+1]
    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.
    
  9. added this to the 1.12.4 milestone on Sep 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions