问题描述
我正在尝试创建一个修改data.table的函数,并希望使用一些非标准的评估,但是我意识到我并不真正知道如何在data.tables中使用它。
我的功能基本上是这样的:
I'm trying to create a function that modifies a data.table and wanted to use some non-standard evaluation but I realised that I don't really know how to work with it inside data.tables.My function is basically something like this:
do_stuff <- function(dt, col) {
copy(dt)[, new_col := some_fun(col)][]
}
,我这样称呼它:
and I want to call it thus:
do_stuff(data, column)
其中列为数据内部存在的列的名称。如果我运行该函数,则会出现错误:
Where "column" is the name of the column that exists inside "data". If I run that function I get an error:
#> Error in some_fun(col) : object 'column' not found
哪个对我说data.table显然是将正确的名称传递给函数(列),但是由于某种原因,找不到它。这是一个最小的可复制示例
Which says to me that data.table is apparently passing the correct name to the function ("column") but for some reason it's not finding it. Here's a minimal reproducible example
library(data.table)
data <- data.table(x = 1:10, y = rnorm(10))
plus <- function(x, y) {
x + y
}
add_one <- function(data, col) {
copy(data)[, z := plus(col, 1)][]
}
add_one(data, y)
#> Error in plus(col, 1): object 'y' not found
使用 deparse(substitute(col))
似乎不起作用,不幸的是:(
Using deparse(substitute(col))
doesn't seem to work, unfortunately :(
add_one <- function(data, col) {
copy(data)[, z := plus(deparse(substitute(col)), 1)][]
}
add_one(data, y)
#> Error in x + y: non-numeric argument to binary operator
推荐答案
通常,引用和评估将起作用:
Generally, quote and eval will work:
library(data.table)
plus <- function(x, y) {
x + y
}
add_one <- function(data, col) {
expr0 = quote(copy(data)[, z := plus(col, 1)][])
expr = do.call(substitute, list(expr0, list(col = substitute(col))))
cat("Evaluated expression:\n"); print(expr); cat("\n")
eval(expr)
}
set.seed(1)
library(magrittr)
data.table(x = 1:10, y = rnorm(10)) %>%
add_one(y)
>
which gives
Evaluated expression:
copy(data)[, `:=`(z, plus(y, 1))][]
x y z
1: 1 -0.6264538 0.3735462
2: 2 0.1836433 1.1836433
3: 3 -0.8356286 0.1643714
4: 4 1.5952808 2.5952808
5: 5 0.3295078 1.3295078
6: 6 -0.8204684 0.1795316
7: 7 0.4874291 1.4874291
8: 8 0.7383247 1.7383247
9: 9 0.5757814 1.5757814
10: 10 -0.3053884 0.6946116
这篇关于将变量名作为参数传递给data.table的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!