My understanding is that the j expression is evaluated for each row in the data table. Therefore, I expect an empty data.table to produce no output. In the 3 cases below, I don't understand why case 2 produces any output or why it produces warnings. I also don't think that test 2 and 3 producing different output could be the desired/intended result
a <- data.table(foo = 0, bar = 1)
b <- a[1==2] #empty table
print("Test 1")
b[, .(foo, bar)] # expected output
print("Test 2")
b[, .(foo, bar, baz = "Hello")] #Unexpected output
print("Test 3")
b[1==2, .(foo, bar, baz = "Hello")] #expected output
Output:
[1] "Test 1"
Empty data.table (0 rows) of 2 cols: foo,bar
[1] "Test 2"
baz
1: Hello
Warning messages:
1: In as.data.table.list(jval) :
Item 1 is of size 0 but maximum size is 1, therefore recycled with 'NA'
2: In as.data.table.list(jval) :
Item 2 is of size 0 but maximum size is 1, therefore recycled with 'NA'
[1] "Test 3"
Empty data.table (0 rows) of 3 cols: foo,bar,baz
> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 16299)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] XML_3.98-1.9 doParallel_1.0.10 iterators_1.0.8 foreach_1.4.4 jsonlite_1.4 curl_2.6 data.table_1.10.5 quantmod_0.4-12 TTR_0.23-2 xts_0.10-0
[11] zoo_1.8-0 RODBC_1.3-15 plotrix_3.6-6 RevoUtilsMath_10.0.0 RevoUtils_10.0.5 RevoMods_11.0.0 MicrosoftML_1.5.0 mrsdeploy_1.1.2 RevoScaleR_9.2.1 lattice_0.20-35
[21] rpart_4.1-11 checkpoint_0.4.0
loaded via a namespace (and not attached):
[1] codetools_0.2-15 CompatibilityAPI_1.1.0 grid_3.4.1 R6_2.2.0 tools_3.4.1 compiler_3.4.1 rtvs_1.0.0.0 mrupdate_1.0.1
My understanding is that the j expression is evaluated for each row in the data table. Therefore, I expect an empty data.table to produce no output. In the 3 cases below, I don't understand why case 2 produces any output or why it produces warnings. I also don't think that test 2 and 3 producing different output could be the desired/intended result
Output: