.SD to data.table in R

When writing some expression that works in j on data.table , .SD does not contain all the columns in the table, but rather only those that use the expression. This is fine for work, but for debugging it is not surprising. What is the best way to see all columns? I can pass all the .SDcols names, but that seems pretty tedious. Example:

 x = data.table(a=1:10, b=10:1, id=1:5) x[,{ browser(); a+1},by=id] Called from: `[.data.table`(x, , { browser() a + 1 }, by = id) Browse[1]> n debug at #1: a + 1 Browse[1]> .SD a 1: 1 2: 6 
+6
source share
1 answer

To make all .SD columns available, you just need to specify it somewhere in your j expression. For example, try the following:

 x[,{.SD; browser(); a+1},by=id] # Called from: `[.data.table`(x, , { # .SD # browser() # a + 1 # }, by = id) Browse[1]> .SD # ab # 1: 1 10 # 2: 6 5 

This works because as described here

[.data.table() [...] looks through an unexpressed j-expression and adds only to the .SD columns to which they refer. If .SD itself is mentioned, it adds all the DT columns.


Alternatively, if you do not want to bear the cost of loading .SD columns for each group calculation, you can always check the currently loaded subset of x by calling x[.I,] . ( .I is a variable that stores the location of the lines in x current group):

 x[,{browser(); a+1},by=id] # Called from: `[.data.table`(x, , { # browser() # a + 1 # }, by = id) Browse[1]> x[.I,] # ab id # 1: 1 10 1 # 2: 6 5 1 
+6
source

Source: https://habr.com/ru/post/945094/


All Articles