Take DT as mtcars data table.
DT <- as.data.table(mtcars)
While taking multiple arguments in 'j' with .SD, if we use dot(.) before j like below code
DT[ , .(lapply(.SD, sum), .N), by = (cyl) ]
the result comes in vertical order with without the column names.
O/P:
cyl V1 N
1: 6 138.2 7
2: 6 1283.2 7
3: 6 856 7
4: 6 25.1 7
5: 6 21.82 7
6: 6 125.84 7
7: 6 4 7
8: 6 3 7
But when I replace that with the dot(.) in 'j' with 'c' like below,
DT[ , c(lapply(.SD, sum), .N), by = (cyl) ]
the result comes in horizontal order.
O/P:
cyl mpg disp hp drat wt qsec vs am gear carb N
6 138.2 1283.2 856 25.10 21.820 125.84 4 3 27 24 7
4 293.3 1156.5 909 44.78 25.143 210.51 10 8 45 17 11
8 211.4 4943.4 2929 45.21 55.989 234.81 0 2 46 49 14
In another case, without lapply, exactly the opposite happens.
DT[ , c(sum(mpg), .N), by = (cyl) ]
gives the output vertically
O/P:
cyl V1
1: 6 138.2
2: 6 7.0
3: 4 293.3
4: 4 11.0
5: 8 211.4
6: 8 14.0
whereas a dot(.) in 'j' gives the output horizonatlly.
DT[ , .(sum(mpg), .N), by = (cyl) ]
O/P:
cyl V1 N
1: 6 138.2 7
2: 4 293.3 11
3: 8 211.4 14
Why does this happen? Why the result is ordered in such way?