Is there an easy way to use a variable containing a data.table
column name (string) in the right hand size of an expression that creates a new data.table
column?
library(data.table)
mtcars = as.data.table(mtcars)
# Say I have the name of a data.table column stored in a variable
colname = "mpg"
# How could i use this variable to create new columns
# My initial attempt:
mtcars[, `:=` (xMPGBiggerThan20 = colname > 20)]
# I think result is based on "mpg" > 20, rather than using the column values
head(mtcars)
#> mpg cyl disp hp drat wt qsec vs am gear carb xMPGBiggerThan20
#> 1: 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 TRUE
#> 2: 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 TRUE
#> 3: 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 TRUE
#> 4: 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 TRUE
#> 5: 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 TRUE
#> 6: 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 TRUE
# How would I make the command equivalent to if I wrote
mtcars[, `:=` (xMPGBiggerThan20 = mpg > 20)]
# Desired result
head(mtcars)
#> mpg cyl disp hp drat wt qsec vs am gear carb xMPGBiggerThan20
#> 1: 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 TRUE
#> 2: 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 TRUE
#> 3: 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 TRUE
#> 4: 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 TRUE
#> 5: 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 FALSE
#> 6: 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 FALSE
Created on 2022-02-14 by the reprex package (v2.0.1)
CodePudding user response:
You can try (data.table 1.14.3
)
mtcars[, xMPGBiggerThan20 := colname > 20, env = list(colname = "mpg")][]
or
mtcars[, `:=` (xMPGBiggerThan20 = colname > 20), env = list(colname = "mpg")][]
If you don't want to update to the development version, you can consider to use
mtcars[, `:=` (xMPGBiggerThan20 = get(colname) > 20)]
PS: You can update to the newest version with data.table::update.dev.pkg()
Ref: https://rdatatable.gitlab.io/data.table/articles/datatable-programming.html
CodePudding user response:
Here is some code that should work
mtcars[["xMPGBiggerThan20"]] <- mtcars[[colname]] > 20
The following also produces an identical result
mtcars$xMPGBiggerThan20 <- mtcars[[colname]] > 20