%函数之间的百分比可能改善

时间:2013-07-01 10:52:22

标签: r data.table

根据data.table ver 1.8.8 %between%定义如下:

> `%between%`
function (x, y) 
between(x, y[1], y[2], incbounds = TRUE)
<bytecode: 0x0000000050912810>
<environment: namespace:data.table>

我认为,通过这个微小的改变,这个函数将被矢量化,

function (x, y) 
    between(x, y[[1]], y[[2]], incbounds = TRUE)

喜欢between

s <- c(2, 5)
d <- c(7, 9)
> between(3, s, d)
[1]  TRUE FALSE

这个想法来自于有一个包含两个向量的列表,这表明了这种可能的用法:

`between2%` <- function(x, lst) between(x, lst[[1]], lst[[2]], incbounds = TRUE)

> 3%between%c(s,d)
[1] TRUE
> 3%between2%list(s,d)
[1]  TRUE FALSE

我的问题是:如果我更换了%between%data.table包中的任何功能都会受到影响吗?我认为它不应该,[[应该像[那样使用原子向量。我对么?感谢

> 3%between2%c(1,5)
[1] TRUE

1 个答案:

答案 0 :(得分:3)

我认为这是一个有趣的问题,因为我想知道一般来说,其他函数对函数的使用有何看法。据我所知,没有办法直接这样做(也许有人可以纠正我?)。但是,我将一些代码放在一起,在其他函数的文本表示中查找函数名称。对于%between%,以下是:

library(data.table)
objs <- objects("package:data.table")
z <- textConnection("funs", open="w")
dump(list=objs, file=z)
close(z)

# find all mentions of `%between%` in `data.table` functions
funs[grep("%between%",funs)] ## only mentions are when %between% is defined

# find all mentions of all `data.table` functions in `data.table` functions
locations <- lapply(objs, function(x) grep(x,funs))
names(locations) <- objs

更新:在进行了一些搜索后,this question/answer似乎还提供了有关如何使用foodweb中的library(mvbutils)以编程方式检测依赖关系的更多信息。< / p>

相关问题