%函数之间的%可能改善 [英] %between% function possible improvement

查看:78
本文介绍了%函数之间的%可能改善的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 > `%between%`
函数(x,y)
在(x,y [1],y [2],incbounds = TRUE之间)
< bytecode:0x0000000050912810&
< environment:namespace:data.table>

我认为,这个微小的变化,这个函数会被矢量化,

$ b (x,y [[1]],y [[2]],incbounds = TRUE)之间的函数(x,y)
$ b <$ c $ p>

之间

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

具有两个向量的列表,其建议我这种可能的用法:

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

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

是:如果我替换%之间的%会影响 data.table 包中的任何功能?我认为它不应该, [[应该使用原子向量 [。我对么?感谢

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


解决方案

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

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

#在数据表中查找%between之间的所有提及函数
funs [grep(%between% ] ##只提到%之间的%定义

#查找`data.table`中所有`data.table`函数的所有提及函数
locations< - lapply (objs,function(x)grep(x,funs))
names(locations)< - objs

UPDATE :在进行更多搜索后, this question / answer 似乎还提供了一些关于如何使用 foodweb 库(mvbutils) / code>。


As per data.table ver 1.8.8 %between% is defined as follow:

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

I thought that, with this tiny change, this function would be vectorised,

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

like between

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

The idea came from having a list with two vectors, which suggested me this possible usage:

`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

My question is: if I replaced %between% would any functionality in data.table package be affected? I think it shouldn't, [[ should work with atomic vector as [ does. Am I correct? thanks

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

解决方案

I thought this was an interesting question because I wondered how one might look, in general, for uses of a function by other functions. As far as I know, there's no way to directly do this (perhaps someone can correct me?). But, I put together a little code that looks for function names in text representations of other functions. For %between%, here's the following:

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

UPDATE: After doing some more searching, this question/answer also seem to provide some more information on how to detect dependencies programmatically using foodweb from library(mvbutils).

这篇关于%函数之间的%可能改善的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆