A specialization of dplyr verbs, as well as a set of custom ones, that build expressions that can be used within a data.table's frame.

Note

Note that since version 0.3.0, it is not possible to load table.express and dtplyr at the same time, since they define the same data.table methods for many dplyr generics.

Bearing in mind that data.tables are also data.frames, we have to consider that other packages may uses dplyr internally without importing data.table. Since dplyr's methods are generic, calls to these methods in such packages would fail. The functions in this package try to detect when this happens and delegate to the data.frame methods with a warning, which can be safely ignored if you know that the error originates from a package that is not meant to work with data.table. To avoid the warning, use options(table.express.warn.cedta = FALSE).

This software package was developed independently of any organization or institution that is or has been associated with the author.

Author

Alexis Sarda-Espinosa

Examples

require("data.table")
#> Loading required package: data.table

data("mtcars")

DT <- as.data.table(mtcars)

# ====================================================================================
# Simple dplyr-like transformations

DT %>%
    group_by(cyl) %>%
    filter(vs == 0, am == 1) %>%
    transmute(mean_mpg = mean(mpg)) %>%
    arrange(-cyl)
#>    cyl mean_mpg
#> 1:   8 15.40000
#> 2:   6 20.56667
#> 3:   4 26.00000

# Equivalent to previous
DT %>%
    start_expr %>%
    transmute(mean_mpg = mean(mpg)) %>%
    where(vs == 0, am == 1) %>%
    group_by(cyl) %>%
    order_by(-cyl) %>%
    end_expr
#>    cyl mean_mpg
#> 1:   8 15.40000
#> 2:   6 20.56667
#> 3:   4 26.00000

# Modification by reference
DT %>%
    where(gear %% 2 != 0, carb %% 2 == 0) %>%
    mutate(wt_squared = wt ^ 2)
#>      mpg cyl  disp  hp drat    wt  qsec vs am gear carb wt_squared
#>  1: 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4         NA
#>  2: 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4         NA
#>  3: 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1         NA
#>  4: 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1         NA
#>  5: 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2  11.833600
#>  6: 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1         NA
#>  7: 14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4  12.744900
#>  8: 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2         NA
#>  9: 22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2         NA
#> 10: 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4         NA
#> 11: 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4         NA
#> 12: 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3         NA
#> 13: 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3         NA
#> 14: 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3         NA
#> 15: 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4  27.562500
#> 16: 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4  29.419776
#> 17: 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4  28.569025
#> 18: 32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1         NA
#> 19: 30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2         NA
#> 20: 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1         NA
#> 21: 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1         NA
#> 22: 15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2  12.390400
#> 23: 15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2  11.799225
#> 24: 13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4  14.745600
#> 25: 19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2  14.784025
#> 26: 27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1         NA
#> 27: 26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2   4.579600
#> 28: 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2   2.289169
#> 29: 15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4  10.048900
#> 30: 19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6   7.672900
#> 31: 15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8  12.744900
#> 32: 21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2         NA
#>      mpg cyl  disp  hp drat    wt  qsec vs am gear carb wt_squared

print(DT)
#>      mpg cyl  disp  hp drat    wt  qsec vs am gear carb wt_squared
#>  1: 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4         NA
#>  2: 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4         NA
#>  3: 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1         NA
#>  4: 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1         NA
#>  5: 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2  11.833600
#>  6: 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1         NA
#>  7: 14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4  12.744900
#>  8: 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2         NA
#>  9: 22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2         NA
#> 10: 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4         NA
#> 11: 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4         NA
#> 12: 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3         NA
#> 13: 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3         NA
#> 14: 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3         NA
#> 15: 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4  27.562500
#> 16: 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4  29.419776
#> 17: 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4  28.569025
#> 18: 32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1         NA
#> 19: 30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2         NA
#> 20: 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1         NA
#> 21: 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1         NA
#> 22: 15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2  12.390400
#> 23: 15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2  11.799225
#> 24: 13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4  14.745600
#> 25: 19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2  14.784025
#> 26: 27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1         NA
#> 27: 26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2   4.579600
#> 28: 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2   2.289169
#> 29: 15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4  10.048900
#> 30: 19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6   7.672900
#> 31: 15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8  12.744900
#> 32: 21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2         NA
#>      mpg cyl  disp  hp drat    wt  qsec vs am gear carb wt_squared

# Deletion by reference
DT %>%
    mutate(wt_squared = NULL) %>%
    print
#>      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#>  1: 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#>  2: 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#>  3: 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#>  4: 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#>  5: 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
#>  6: 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#>  7: 14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
#>  8: 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
#>  9: 22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
#> 10: 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
#> 11: 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
#> 12: 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
#> 13: 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
#> 14: 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
#> 15: 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
#> 16: 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
#> 17: 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
#> 18: 32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
#> 19: 30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
#> 20: 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
#> 21: 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
#> 22: 15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
#> 23: 15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
#> 24: 13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
#> 25: 19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
#> 26: 27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
#> 27: 26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
#> 28: 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
#> 29: 15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
#> 30: 19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
#> 31: 15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
#> 32: 21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
#>      mpg cyl  disp  hp drat    wt  qsec vs am gear carb

# Support for tidyslect helpers

DT %>%
    select(ends_with("t"))
#>     drat    wt
#>  1: 3.90 2.620
#>  2: 3.90 2.875
#>  3: 3.85 2.320
#>  4: 3.08 3.215
#>  5: 3.15 3.440
#>  6: 2.76 3.460
#>  7: 3.21 3.570
#>  8: 3.69 3.190
#>  9: 3.92 3.150
#> 10: 3.92 3.440
#> 11: 3.92 3.440
#> 12: 3.07 4.070
#> 13: 3.07 3.730
#> 14: 3.07 3.780
#> 15: 2.93 5.250
#> 16: 3.00 5.424
#> 17: 3.23 5.345
#> 18: 4.08 2.200
#> 19: 4.93 1.615
#> 20: 4.22 1.835
#> 21: 3.70 2.465
#> 22: 2.76 3.520
#> 23: 3.15 3.435
#> 24: 3.73 3.840
#> 25: 3.08 3.845
#> 26: 4.08 1.935
#> 27: 4.43 2.140
#> 28: 3.77 1.513
#> 29: 4.22 3.170
#> 30: 3.62 2.770
#> 31: 3.54 3.570
#> 32: 4.11 2.780
#>     drat    wt

# ====================================================================================
# Helpers to transform a subset of data

# Like DT[, (whole) := lapply(.SD, as.integer), .SDcols = whole]
whole <- names(DT)[sapply(DT, function(x) { all(x %% 1 == 0) })]
DT %>%
    mutate_sd(as.integer, .SDcols = whole)
#>      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#>  1: 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#>  2: 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#>  3: 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#>  4: 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#>  5: 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
#>  6: 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#>  7: 14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
#>  8: 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
#>  9: 22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
#> 10: 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
#> 11: 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
#> 12: 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
#> 13: 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
#> 14: 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
#> 15: 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
#> 16: 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
#> 17: 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
#> 18: 32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
#> 19: 30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
#> 20: 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
#> 21: 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
#> 22: 15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
#> 23: 15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
#> 24: 13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
#> 25: 19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
#> 26: 27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
#> 27: 26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
#> 28: 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
#> 29: 15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
#> 30: 19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
#> 31: 15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
#> 32: 21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
#>      mpg cyl  disp  hp drat    wt  qsec vs am gear carb

sapply(DT, class)
#>       mpg       cyl      disp        hp      drat        wt      qsec        vs 
#> "numeric" "integer" "numeric" "integer" "numeric" "numeric" "numeric" "integer" 
#>        am      gear      carb 
#> "integer" "integer" "integer" 

# Like DT[, lapply(.SD, fun), .SDcols = ...]
DT %>%
    transmute_sd((.COL - mean(.COL)) / sd(.COL),
                 .SDcols = setdiff(names(DT), whole))
#>             mpg        disp        drat           wt        qsec
#>  1:  0.15088482 -0.57061982  0.56751369 -0.610399567 -0.77716515
#>  2:  0.15088482 -0.57061982  0.56751369 -0.349785269 -0.46378082
#>  3:  0.44954345 -0.99018209  0.47399959 -0.917004624  0.42600682
#>  4:  0.21725341  0.22009369 -0.96611753 -0.002299538  0.89048716
#>  5: -0.23073453  1.04308123 -0.83519779  0.227654255 -0.46378082
#>  6: -0.33028740 -0.04616698 -1.56460776  0.248094592  1.32698675
#>  7: -0.96078893  1.04308123 -0.72298087  0.360516446 -1.12412636
#>  8:  0.71501778 -0.67793094  0.17475447 -0.027849959  1.20387148
#>  9:  0.44954345 -0.72553512  0.60491932 -0.068730634  2.82675459
#> 10: -0.14777380 -0.50929918  0.60491932  0.227654255  0.25252621
#> 11: -0.38006384 -0.50929918  0.60491932  0.227654255  0.58829513
#> 12: -0.61235388  0.36371309 -0.98482035  0.871524874 -0.25112717
#> 13: -0.46302456  0.36371309 -0.98482035  0.524039143 -0.13920420
#> 14: -0.81145962  0.36371309 -0.98482035  0.575139986  0.08464175
#> 15: -1.60788262  1.94675381 -1.24665983  2.077504765  0.07344945
#> 16: -1.60788262  1.84993175 -1.11574009  2.255335698 -0.01608893
#> 17: -0.89442035  1.68856165 -0.68557523  2.174596366 -0.23993487
#> 18:  2.04238943 -1.22658929  0.90416444 -1.039646647  0.90727560
#> 19:  1.71054652 -1.25079481  2.49390411 -1.637526508  0.37564148
#> 20:  2.29127162 -1.28790993  1.16600392 -1.412682800  1.14790999
#> 21:  0.23384555 -0.89255318  0.19345729 -0.768812180  1.20946763
#> 22: -0.76168319  0.70420401 -1.56460776  0.309415603 -0.54772305
#> 23: -0.81145962  0.59124494 -0.83519779  0.222544170 -0.30708866
#> 24: -1.12671039  0.96239618  0.24956575  0.636460997 -1.36476075
#> 25: -0.14777380  1.36582144 -0.96611753  0.641571082 -0.44699237
#> 26:  1.19619000 -1.22416874  0.90416444 -1.310481114  0.58829513
#> 27:  0.98049211 -0.89093948  1.55876313 -1.100967659 -0.64285758
#> 28:  1.71054652 -1.09426581  0.32437703 -1.741772228 -0.53093460
#> 29: -0.71190675  0.97046468  1.16600392 -0.048290296 -1.87401028
#> 30: -0.06481307 -0.69164740  0.04383473 -0.457097039 -1.31439542
#> 31: -0.84464392  0.56703942 -0.10578782  0.360516446 -1.81804880
#> 32:  0.21725341 -0.88529152  0.96027290 -0.446876870  0.42041067
#>             mpg        disp        drat           wt        qsec

# Filter several with the same condition
DT %>%
    filter_sd(.COL == 1, .SDcols = c("vs", "am"))
#>     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> 1: 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#> 2: 32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
#> 3: 30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
#> 4: 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
#> 5: 27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
#> 6: 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
#> 7: 21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2

# Using secondary indices, i.e. DT[.(4, 5), on = .(cyl, gear)]
DT %>%
    filter_on(cyl = 4, gear = 5) # note we don't use ==
#>     mpg cyl  disp  hp drat    wt qsec vs am gear carb
#> 1: 26.0   4 120.3  91 4.43 2.140 16.7  0  1    5    2
#> 2: 30.4   4  95.1 113 3.77 1.513 16.9  1  1    5    2

scale_undim <- function(...) {
    as.numeric(scale(...)) # remove dimensions
}

# Chaining
DT %>%
    start_expr %>%
    mutate_sd(as.integer, .SDcols = whole) %>%
    chain %>%
    filter_sd(.COL == 1, .SDcols = c("vs", "am"), .collapse = `|`) %>%
    transmute_sd(scale_undim, .SDcols = !is.integer(.COL)) %>%
    end_expr
#>             mpg        disp        drat          wt        qsec
#>  1: -0.39184157  0.07107789  0.03955921 -0.09987243 -0.87064903
#>  2: -0.39184157  0.07107789  0.03955921  0.29103270 -0.60164850
#>  3: -0.05835938 -0.61147991 -0.07032748 -0.55976081  0.16212085
#>  4: -0.31773442  1.35743682 -1.76258247  0.81223954  0.56081807
#>  5: -0.92911843  0.92427514 -2.46585727  1.18781505  0.93549737
#>  6:  0.23806923 -0.10349939 -0.42196488  0.77391551  0.82981859
#>  7: -0.05835938 -0.18094345  0.08351388  0.71259705  2.22285704
#>  8: -0.72532376  0.17083634  0.08351388  1.15715583  0.01320985
#>  9: -0.98469880  0.17083634  0.08351388  1.15715583  0.30142470
#> 10:  1.72021229 -0.99607497  0.43515128 -0.74371617  0.57522881
#> 11:  1.34967653 -1.03545331  2.30322498 -1.64049852  0.11888863
#> 12:  1.99811411 -1.09583342  0.74283401 -1.30324704  0.78178279
#> 13: -0.29920763 -0.45265396 -0.39998754 -0.33748143  0.83462217
#> 14:  0.77534609 -0.99213714  0.43515128 -1.14995091  0.30142470
#> 15:  0.53449784 -0.45002873  1.20435810 -0.83569385 -0.75536309
#> 16:  1.34967653 -0.78080674 -0.24614618 -1.79686057 -0.65929147
#> 17: -1.35523456  2.57816520  0.74283401  0.74325628 -1.81215088
#> 18: -0.63268982 -0.12581378 -0.57580624  0.13007177 -1.33179279
#> 19: -1.50344887  1.92185962 -0.75162494  1.35644079 -1.76411507
#> 20: -0.31773442 -0.44084046  0.50108330  0.14540138  0.15731727

# The previous is quivalent to
DT[, (whole) := lapply(.SD, as.integer), .SDcols = whole
   ][vs == 1 | am == 1,
     lapply(.SD, scale_undim),
     .SDcols = names(DT)[sapply(DT, Negate(is.integer))]]
#>             mpg        disp        drat          wt        qsec
#>  1: -0.39184157  0.07107789  0.03955921 -0.09987243 -0.87064903
#>  2: -0.39184157  0.07107789  0.03955921  0.29103270 -0.60164850
#>  3: -0.05835938 -0.61147991 -0.07032748 -0.55976081  0.16212085
#>  4: -0.31773442  1.35743682 -1.76258247  0.81223954  0.56081807
#>  5: -0.92911843  0.92427514 -2.46585727  1.18781505  0.93549737
#>  6:  0.23806923 -0.10349939 -0.42196488  0.77391551  0.82981859
#>  7: -0.05835938 -0.18094345  0.08351388  0.71259705  2.22285704
#>  8: -0.72532376  0.17083634  0.08351388  1.15715583  0.01320985
#>  9: -0.98469880  0.17083634  0.08351388  1.15715583  0.30142470
#> 10:  1.72021229 -0.99607497  0.43515128 -0.74371617  0.57522881
#> 11:  1.34967653 -1.03545331  2.30322498 -1.64049852  0.11888863
#> 12:  1.99811411 -1.09583342  0.74283401 -1.30324704  0.78178279
#> 13: -0.29920763 -0.45265396 -0.39998754 -0.33748143  0.83462217
#> 14:  0.77534609 -0.99213714  0.43515128 -1.14995091  0.30142470
#> 15:  0.53449784 -0.45002873  1.20435810 -0.83569385 -0.75536309
#> 16:  1.34967653 -0.78080674 -0.24614618 -1.79686057 -0.65929147
#> 17: -1.35523456  2.57816520  0.74283401  0.74325628 -1.81215088
#> 18: -0.63268982 -0.12581378 -0.57580624  0.13007177 -1.33179279
#> 19: -1.50344887  1.92185962 -0.75162494  1.35644079 -1.76411507
#> 20: -0.31773442 -0.44084046  0.50108330  0.14540138  0.15731727

# Alternative to keep all columns (*copying* non-scaled ones)
scale_non_integers <- function(x) {
    if (is.integer(x)) x else scale_undim(x)
}

DT %>%
    filter_sd(.COL == 1, .SDcols = c("vs", "am"), .collapse = `|`) %>%
    transmute_sd(everything(), scale_non_integers)
#>             mpg cyl        disp  hp        drat          wt        qsec vs am
#>  1: -0.39184157   6  0.07107789 110  0.03955921 -0.09987243 -0.87064903  0  1
#>  2: -0.39184157   6  0.07107789 110  0.03955921  0.29103270 -0.60164850  0  1
#>  3: -0.05835938   4 -0.61147991  93 -0.07032748 -0.55976081  0.16212085  1  1
#>  4: -0.31773442   6  1.35743682 110 -1.76258247  0.81223954  0.56081807  1  0
#>  5: -0.92911843   6  0.92427514 105 -2.46585727  1.18781505  0.93549737  1  0
#>  6:  0.23806923   4 -0.10349939  62 -0.42196488  0.77391551  0.82981859  1  0
#>  7: -0.05835938   4 -0.18094345  95  0.08351388  0.71259705  2.22285704  1  0
#>  8: -0.72532376   6  0.17083634 123  0.08351388  1.15715583  0.01320985  1  0
#>  9: -0.98469880   6  0.17083634 123  0.08351388  1.15715583  0.30142470  1  0
#> 10:  1.72021229   4 -0.99607497  66  0.43515128 -0.74371617  0.57522881  1  1
#> 11:  1.34967653   4 -1.03545331  52  2.30322498 -1.64049852  0.11888863  1  1
#> 12:  1.99811411   4 -1.09583342  65  0.74283401 -1.30324704  0.78178279  1  1
#> 13: -0.29920763   4 -0.45265396  97 -0.39998754 -0.33748143  0.83462217  1  0
#> 14:  0.77534609   4 -0.99213714  66  0.43515128 -1.14995091  0.30142470  1  1
#> 15:  0.53449784   4 -0.45002873  91  1.20435810 -0.83569385 -0.75536309  0  1
#> 16:  1.34967653   4 -0.78080674 113 -0.24614618 -1.79686057 -0.65929147  1  1
#> 17: -1.35523456   8  2.57816520 264  0.74283401  0.74325628 -1.81215088  0  1
#> 18: -0.63268982   6 -0.12581378 175 -0.57580624  0.13007177 -1.33179279  0  1
#> 19: -1.50344887   8  1.92185962 335 -0.75162494  1.35644079 -1.76411507  0  1
#> 20: -0.31773442   4 -0.44084046 109  0.50108330  0.14540138  0.15731727  1  1
#>     gear carb
#>  1:    4    4
#>  2:    4    4
#>  3:    4    1
#>  4:    3    1
#>  5:    3    1
#>  6:    4    2
#>  7:    4    2
#>  8:    4    4
#>  9:    4    4
#> 10:    4    1
#> 11:    4    2
#> 12:    4    1
#> 13:    3    1
#> 14:    4    1
#> 15:    5    2
#> 16:    5    2
#> 17:    5    4
#> 18:    5    6
#> 19:    5    8
#> 20:    4    2

# Without copying non-scaled
DT %>%
    where(vs == 1 | am == 1) %>%
    mutate_sd(scale, .SDcols = names(DT)[sapply(DT, Negate(is.integer))])
#>             mpg cyl         disp  hp        drat          wt        qsec vs am
#>  1: -0.39184157   6   0.07107789 110  0.03955921 -0.09987243 -0.87064903  0  1
#>  2: -0.39184157   6   0.07107789 110  0.03955921  0.29103270 -0.60164850  0  1
#>  3: -0.05835938   4  -0.61147991  93 -0.07032748 -0.55976081  0.16212085  1  1
#>  4: -0.31773442   6   1.35743682 110 -1.76258247  0.81223954  0.56081807  1  0
#>  5: 18.70000000   8 360.00000000 175  3.15000000  3.44000000 17.02000000  0  0
#>  6: -0.92911843   6   0.92427514 105 -2.46585727  1.18781505  0.93549737  1  0
#>  7: 14.30000000   8 360.00000000 245  3.21000000  3.57000000 15.84000000  0  0
#>  8:  0.23806923   4  -0.10349939  62 -0.42196488  0.77391551  0.82981859  1  0
#>  9: -0.05835938   4  -0.18094345  95  0.08351388  0.71259705  2.22285704  1  0
#> 10: -0.72532376   6   0.17083634 123  0.08351388  1.15715583  0.01320985  1  0
#> 11: -0.98469880   6   0.17083634 123  0.08351388  1.15715583  0.30142470  1  0
#> 12: 16.40000000   8 275.80000000 180  3.07000000  4.07000000 17.40000000  0  0
#> 13: 17.30000000   8 275.80000000 180  3.07000000  3.73000000 17.60000000  0  0
#> 14: 15.20000000   8 275.80000000 180  3.07000000  3.78000000 18.00000000  0  0
#> 15: 10.40000000   8 472.00000000 205  2.93000000  5.25000000 17.98000000  0  0
#> 16: 10.40000000   8 460.00000000 215  3.00000000  5.42400000 17.82000000  0  0
#> 17: 14.70000000   8 440.00000000 230  3.23000000  5.34500000 17.42000000  0  0
#> 18:  1.72021229   4  -0.99607497  66  0.43515128 -0.74371617  0.57522881  1  1
#> 19:  1.34967653   4  -1.03545331  52  2.30322498 -1.64049852  0.11888863  1  1
#> 20:  1.99811411   4  -1.09583342  65  0.74283401 -1.30324704  0.78178279  1  1
#> 21: -0.29920763   4  -0.45265396  97 -0.39998754 -0.33748143  0.83462217  1  0
#> 22: 15.50000000   8 318.00000000 150  2.76000000  3.52000000 16.87000000  0  0
#> 23: 15.20000000   8 304.00000000 150  3.15000000  3.43500000 17.30000000  0  0
#> 24: 13.30000000   8 350.00000000 245  3.73000000  3.84000000 15.41000000  0  0
#> 25: 19.20000000   8 400.00000000 175  3.08000000  3.84500000 17.05000000  0  0
#> 26:  0.77534609   4  -0.99213714  66  0.43515128 -1.14995091  0.30142470  1  1
#> 27:  0.53449784   4  -0.45002873  91  1.20435810 -0.83569385 -0.75536309  0  1
#> 28:  1.34967653   4  -0.78080674 113 -0.24614618 -1.79686057 -0.65929147  1  1
#> 29: -1.35523456   8   2.57816520 264  0.74283401  0.74325628 -1.81215088  0  1
#> 30: -0.63268982   6  -0.12581378 175 -0.57580624  0.13007177 -1.33179279  0  1
#> 31: -1.50344887   8   1.92185962 335 -0.75162494  1.35644079 -1.76411507  0  1
#> 32: -0.31773442   4  -0.44084046 109  0.50108330  0.14540138  0.15731727  1  1
#>             mpg cyl         disp  hp        drat          wt        qsec vs am
#>     gear carb
#>  1:    4    4
#>  2:    4    4
#>  3:    4    1
#>  4:    3    1
#>  5:    3    2
#>  6:    3    1
#>  7:    3    4
#>  8:    4    2
#>  9:    4    2
#> 10:    4    4
#> 11:    4    4
#> 12:    3    3
#> 13:    3    3
#> 14:    3    3
#> 15:    3    4
#> 16:    3    4
#> 17:    3    4
#> 18:    4    1
#> 19:    4    2
#> 20:    4    1
#> 21:    3    1
#> 22:    3    2
#> 23:    3    2
#> 24:    3    4
#> 25:    3    2
#> 26:    4    1
#> 27:    5    2
#> 28:    5    2
#> 29:    5    4
#> 30:    5    6
#> 31:    5    8
#> 32:    4    2
#>     gear carb

print(DT)
#>             mpg cyl         disp  hp        drat          wt        qsec vs am
#>  1: -0.39184157   6   0.07107789 110  0.03955921 -0.09987243 -0.87064903  0  1
#>  2: -0.39184157   6   0.07107789 110  0.03955921  0.29103270 -0.60164850  0  1
#>  3: -0.05835938   4  -0.61147991  93 -0.07032748 -0.55976081  0.16212085  1  1
#>  4: -0.31773442   6   1.35743682 110 -1.76258247  0.81223954  0.56081807  1  0
#>  5: 18.70000000   8 360.00000000 175  3.15000000  3.44000000 17.02000000  0  0
#>  6: -0.92911843   6   0.92427514 105 -2.46585727  1.18781505  0.93549737  1  0
#>  7: 14.30000000   8 360.00000000 245  3.21000000  3.57000000 15.84000000  0  0
#>  8:  0.23806923   4  -0.10349939  62 -0.42196488  0.77391551  0.82981859  1  0
#>  9: -0.05835938   4  -0.18094345  95  0.08351388  0.71259705  2.22285704  1  0
#> 10: -0.72532376   6   0.17083634 123  0.08351388  1.15715583  0.01320985  1  0
#> 11: -0.98469880   6   0.17083634 123  0.08351388  1.15715583  0.30142470  1  0
#> 12: 16.40000000   8 275.80000000 180  3.07000000  4.07000000 17.40000000  0  0
#> 13: 17.30000000   8 275.80000000 180  3.07000000  3.73000000 17.60000000  0  0
#> 14: 15.20000000   8 275.80000000 180  3.07000000  3.78000000 18.00000000  0  0
#> 15: 10.40000000   8 472.00000000 205  2.93000000  5.25000000 17.98000000  0  0
#> 16: 10.40000000   8 460.00000000 215  3.00000000  5.42400000 17.82000000  0  0
#> 17: 14.70000000   8 440.00000000 230  3.23000000  5.34500000 17.42000000  0  0
#> 18:  1.72021229   4  -0.99607497  66  0.43515128 -0.74371617  0.57522881  1  1
#> 19:  1.34967653   4  -1.03545331  52  2.30322498 -1.64049852  0.11888863  1  1
#> 20:  1.99811411   4  -1.09583342  65  0.74283401 -1.30324704  0.78178279  1  1
#> 21: -0.29920763   4  -0.45265396  97 -0.39998754 -0.33748143  0.83462217  1  0
#> 22: 15.50000000   8 318.00000000 150  2.76000000  3.52000000 16.87000000  0  0
#> 23: 15.20000000   8 304.00000000 150  3.15000000  3.43500000 17.30000000  0  0
#> 24: 13.30000000   8 350.00000000 245  3.73000000  3.84000000 15.41000000  0  0
#> 25: 19.20000000   8 400.00000000 175  3.08000000  3.84500000 17.05000000  0  0
#> 26:  0.77534609   4  -0.99213714  66  0.43515128 -1.14995091  0.30142470  1  1
#> 27:  0.53449784   4  -0.45002873  91  1.20435810 -0.83569385 -0.75536309  0  1
#> 28:  1.34967653   4  -0.78080674 113 -0.24614618 -1.79686057 -0.65929147  1  1
#> 29: -1.35523456   8   2.57816520 264  0.74283401  0.74325628 -1.81215088  0  1
#> 30: -0.63268982   6  -0.12581378 175 -0.57580624  0.13007177 -1.33179279  0  1
#> 31: -1.50344887   8   1.92185962 335 -0.75162494  1.35644079 -1.76411507  0  1
#> 32: -0.31773442   4  -0.44084046 109  0.50108330  0.14540138  0.15731727  1  1
#>             mpg cyl         disp  hp        drat          wt        qsec vs am
#>     gear carb
#>  1:    4    4
#>  2:    4    4
#>  3:    4    1
#>  4:    3    1
#>  5:    3    2
#>  6:    3    1
#>  7:    3    4
#>  8:    4    2
#>  9:    4    2
#> 10:    4    4
#> 11:    4    4
#> 12:    3    3
#> 13:    3    3
#> 14:    3    3
#> 15:    3    4
#> 16:    3    4
#> 17:    3    4
#> 18:    4    1
#> 19:    4    2
#> 20:    4    1
#> 21:    3    1
#> 22:    3    2
#> 23:    3    2
#> 24:    3    4
#> 25:    3    2
#> 26:    4    1
#> 27:    5    2
#> 28:    5    2
#> 29:    5    4
#> 30:    5    6
#> 31:    5    8
#> 32:    4    2
#>     gear carb