是否可以使用包含列名的变量作为表达式中的字符串来创建新的 data.table 列
Is it possible to use a variable containing column names as strings in expressions for creating new data.table columns
是否有一种简单的方法来使用包含 data.table
列名称(字符串)的变量,其大小为创建新 data.table
列的表达式的右侧大小?
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
由 reprex package (v2.0.1)
于 2022-02-14 创建
你可以试试(data.table 1.14.3
)
mtcars[, xMPGBiggerThan20 := colname > 20, env = list(colname = "mpg")][]
或
mtcars[, `:=` (xMPGBiggerThan20 = colname > 20), env = list(colname = "mpg")][]
如果不想更新到开发版,可以考虑使用
mtcars[, `:=` (xMPGBiggerThan20 = get(colname) > 20)]
PS:您可以通过data.table::update.dev.pkg()
更新到最新版本
参考:https://rdatatable.gitlab.io/data.table/articles/datatable-programming.html
这是一些应该可以工作的代码
mtcars[["xMPGBiggerThan20"]] <- mtcars[[colname]] > 20
下面也产生相同的结果
mtcars$xMPGBiggerThan20 <- mtcars[[colname]] > 20
您也可以使用.SD。
mtcars[, `:=` (xMPGBiggerThan20 = .SD > 20), .SDcols = colname]
是否有一种简单的方法来使用包含 data.table
列名称(字符串)的变量,其大小为创建新 data.table
列的表达式的右侧大小?
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
由 reprex package (v2.0.1)
于 2022-02-14 创建你可以试试(data.table 1.14.3
)
mtcars[, xMPGBiggerThan20 := colname > 20, env = list(colname = "mpg")][]
或
mtcars[, `:=` (xMPGBiggerThan20 = colname > 20), env = list(colname = "mpg")][]
如果不想更新到开发版,可以考虑使用
mtcars[, `:=` (xMPGBiggerThan20 = get(colname) > 20)]
PS:您可以通过data.table::update.dev.pkg()
参考:https://rdatatable.gitlab.io/data.table/articles/datatable-programming.html
这是一些应该可以工作的代码
mtcars[["xMPGBiggerThan20"]] <- mtcars[[colname]] > 20
下面也产生相同的结果
mtcars$xMPGBiggerThan20 <- mtcars[[colname]] > 20
您也可以使用.SD。
mtcars[, `:=` (xMPGBiggerThan20 = .SD > 20), .SDcols = colname]