data.table 1.8.x mean() function auto removing NA?
问题 Today I found out a bug in my program due to data.table auto remove NA for mean for example: > a<-data.table(a=c(NA,NA,FALSE,FALSE), b=c(1,1,2,2)) > a > a[,list(mean(a), sum(a)),by=b] b V1 V2 1: 1 0 NA // Why V1 = 0 here? I had expected NA 2: 2 0 0 > mean(c(NA,NA,FALSE,FALSE)) [1] NA > mean(c(NA,NA)) [1] NA > mean(c(FALSE,FALSE)) [1] 0 Is this the intended behaviour? 回答1: This isn't intended. Looks like a problem with optimization ... > a[,list(mean(a), sum(a)),by=b] b V1 V2 1: 1 0 NA 2: 2 0