Repository navigation
un-necessary warning for dcast on int columns with fun.aggregate=min/max #5512
Description
Activity
Tests you found are different use cases, where this warning is good. They come from base R rather than data.table. I don't see exactly how we call R from dcast that those warnings are raised, that should be checked, but to suppress the warning we probably have to catch warnings, and re-raise any, excluding this particular one.
You may avoid the warning by explicitly providing the
fillargument, e.g.,dcast(DT, chr ~ ., list(min, max), value.var="int", fill = NA_integer_).The reason for this warning is that data.table is guessing what should be filled using
DT[0][, max(col)], which returnsInf, a double type. However, min/max applies on an integer actually gets integer type. So the C-level code will coerce the double Inf into an integer, which raises the warning.The root cause of this is that,
minormaxreturns different types forinteger()and any non-zero-length integer, includingNA_integer_.Probably we should suppress the warning here:
Lines 28 to 30 in cb8aeff
if (TYPEOF(thisfill) != thistype) { thisfill = PROTECT(coerceVector(thisfill, thistype)); nprotect++; } Reacted by Toby Dylan Hocking, Jan Gorecki, Matt Dowle and Michael Chiricothis is related to #5390 where there is an example with fill=NULL, where dcast uses fun.aggregate to compute a fill value, but the fill value is actually not used/needed.
For example, below the fill value is actually needed and used in three places, so the warning is reasonable.
> dcast(DT, num ~ chr, min, value.var="int") Key: <num> num a b <num> <int> <int> 1: 1 1 NA 2: 2 NA 2 3: 3 NA 3 Warning message: In dcast.data.table(DT, num ~ chr, min, value.var = "int") : NAs introduced by coercion to integer range
However in the code below the fill value is not actually used, so getting this warning is confusing, because the fill value is computed but not used:
> dcast(DT, . ~ chr, min, value.var="int") Key: <.> . a b <char> <int> <int> 1: . 1 2 Warning message: In dcast.data.table(DT, . ~ chr, min, value.var = "int") : NAs introduced by coercion to integer range
Therefore I would say that the fix should be to update dcast so that it only computes the default fill value if it is actually needed in the output.
- added a commit that references this issue
on Mar 14, 2024
Hi! I noticed a warning which I think is potentially confusing and should be removed:
The result above is as expected, so I would suggest removing this warning to avoid confusion. (because of this warning, I mistakenly thought that the result was not computed correctly, but in fact the result is fine)
It seems that this issue is specific to when
value.varis type integer, andfun.aggregateisminormax. No warning happens when we do min/max on numeric column:And no warning happens when we do mean/sd on int column:
I searched the tests for this warning message and I found: