Skip to content

dcast only computes default fill if necessary#5549

Merged
MichaelChirico merged 32 commits intomasterfrom
fix5512
Mar 14, 2024
Merged

dcast only computes default fill if necessary#5549
MichaelChirico merged 32 commits intomasterfrom
fix5512

Conversation

@tdhock
Copy link
Copy Markdown
Member

@tdhock tdhock commented Nov 30, 2022

Closes #5512
Closes #5390

In current master, dcast(fill=NULL) always computes a default fill value, even when there are no missing cells. For example this is the result of a new test case using current master

> DT <- data.table(chr=c("a","b","b"), int=1:3)[, num := as.numeric(int)]
> 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

In the code above it is normal to compute the fill value (fill_value=as.integer(min(integer())) which is NA) because it is used three times.

However the code below gives the following result using current master, indicating that a default fill value is computed, even though it is 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

Using this branch we get the output below (no warning), indicating that no default fill value was computed, because it is not necessary:

> dcast(DT, . ~ chr, min, value.var="int")
Key: <.>
        .     a     b
   <char> <int> <int>
1:      .     1     2
> 

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

Labels

reshape dcast melt

Projects

None yet

6 participants