Within my code, I would like to programmatically select some variables and select and rename some others in a hard coded way. I know that I could achieve this in two steps with setnames()
, yet I am curious how to do it in a single step.
I think I am quite close to it via .SDcols
. However, when I try to combine .SD
with the renamed column, the ".SDcols
columns" are prefixed with ".SD.". How can the prefix be avoided?
library(data.table)
dt <- as.data.table(mtcars)[1:5]
dt
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> 1: 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#> 2: 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#> 3: 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#> 4: 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#> 5: 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
my_vars <- c("cyl", "vs")
# with .SDcol
dt[, .(.SD, z = gear), .SDcol = my_vars]
#> .SD.cyl .SD.vs z # Note the prefix that had been added to the .SDcols
#> 1: 6 0 4
#> 2: 6 0 4
#> 3: 4 1 4
#> 4: 6 1 3
#> 5: 8 0 3
# with named vector
all_vars <- c(my_vars, z = "gear")
dt[, ..all_vars]
#> cyl vs gear
#> 1: 6 0 4
#> 2: 6 0 4
#> 3: 4 1 4
#> 4: 6 1 3
#> 5: 8 0 3
I assume this is because you wrap .SD
in list
(.()
). The list(.SD)
generates a list
containing .SD
, instead of only the .SD
. This then messes with the naming.
Check str
of .SD
wrapped in list
:
dt[, str(.(.SD)), .SDcol = my_vars]
# List of 1
# $ :Classes ‘data.table’ and 'data.frame': 5 obs. of 2 variables:
# ..$ cyl: num [1:5] 6 6 4 6 8
# ..$ vs : num [1:5] 0 0 1 1 0
Corresponding output has the .SD.
prefix:
dt[ , .(.SD), .SDcol = my_vars]
# .SD.cyl .SD.vs
# 1: 6 0
# 2: 6 0
# 3: 4 1
# 4: 6 1
# 5: 8 0
Check str
of .SD
only:
dt[, str(.SD), .SDcol = my_vars]
# Classes ‘data.table’ and 'data.frame': 5 obs. of 2 variables:
# $ cyl: num 6 6 4 6 8
# $ vs : num 0 0 1 1 0
Given the basic property of j
- "As long as j
returns a list, each element of the list becomes a column in the resulting data.table
" - and that .SD
already is a list
(check dt[ , is.list(.SD)]
), we can use c
to combine .SD
with other list elements, e.g. your renamed column wrapped in list
:
dt[, c(.SD, .(z = gear)), .SDcol = my_vars]
# cyl vs z
# 1: 6 0 4
# 2: 6 0 4
# 3: 4 1 4
# 4: 6 1 3
# 5: 8 0 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With