从括号内反映data.table colulmn名称 [英] Refencing data.table colulmn names from within brackets
问题描述
我想创建一个函数startswith在数据表中的括号内使用,它应该返回一个包含以所提供的字符开头的列名的字符向量,例如
DT < - data.table(x = 1,y = 2,z1 = 1,z2 = 2)
#语法DT [ ,startswith(z)]相当于
DT [,。(z1,z2)]
#返回
z1 z2
1:1 2
我熟悉grep搜索文本表达式,但是我找不到一种方法来引用列名DT从一个方括号内。我尝试的一个解决方案是使用ls()和与DT相关的环境来列出DT中的所有列,但我还没有找到一种方法来引用括号内的这个环境。 / p>
目标是为grep创建一个用于方便的函数的包装,我不想在括号内指定DT。
当然有一个更惯用的方法,但这是我想出的:
startswith< - function(pattern =z){
re< - paste0(^,pattern)
call_info< - deparse(sys.calls()[[1]])
if(grepl((^。+ \\() \\\)$),call_info)){
this_name < - sub((^。+ \\()(。+)(\\)$),\ \2,call_info)
} else {
this_name < - strsplit(call_info,\\ [)[[1]] [1]
}
this< - copy(get(this_name))
this_names< - names(this)
eval.parent(grep(re,this_names))
}
library(data.table)
DT< - data.table(x = 1,y = 2,z1 = 1,z2 = 2)
##
R& ; DT [,。(z1,z2)]
z1 z2
1:1 2
##
R& DT [,startswith(),with = F]
z1 z2
1:1 2
$ b b
我不得不添加 if(){} else {}
的功能,例如
Foo < - function(gt){
f< - gt [,startswith(),with = F ]
#{do something interesting with f}
f
}
##
R> Foo(DT)
z1 z2
1:1 2
我认为这是一个有趣的问题,据我所知,R没有一个概念 C ++中的这个
指针,但它在这种情况下肯定是有用的。基本上,我的所有我的黑客与 sys.call
, get
,等...只是这样我可以检索调用对象的名称。
I'd like to create a function "startswith' to be used within brackets in data.table. It should return a character vector containing the column names that begin with the character provided. For example
DT <- data.table(x=1, y=2, z1=1, z2=2)
# the syntax DT[, startswith("z")] is equivalent to
DT[, .(z1, z2)]
# returns
z1 z2
1: 1 2
I'm familiar with grep to search for text expressions, but am having trouble finding a way to refer to the column names of DT from within the brackets. One solution I attempted was to use ls() and the environment associated with DT to list all of the columns in DT, but I haven't found a way to refer to this environment from within the brackets.
The goal is to create a wrapper for grep to be used as a convenience function. I don't want to have to specify the DT from within the brackets.
Surely there is a more idiomatic approach, but this is what I came up with:
startswith <- function(pattern = "z") {
re <- paste0("^", pattern)
call_info <- deparse(sys.calls()[[1]])
if (grepl("(^.+\\()(.+)(\\)$)",call_info)) {
this_name <- sub("(^.+\\()(.+)(\\)$)","\\2",call_info)
} else {
this_name <- strsplit(call_info,"\\[")[[1]][1]
}
this <- copy(get(this_name))
this_names <- names(this)
eval.parent(grep(re,this_names))
}
library(data.table)
DT <- data.table(x=1, y=2, z1=1, z2=2)
##
R> DT[,.(z1, z2)]
z1 z2
1: 1 2
##
R> DT[,startswith(), with=F]
z1 z2
1: 1 2
I had to add in that if () {} else {}
block so that this could be used inside of functions, e.g.
Foo <- function(gt) {
f <- gt[,startswith(),with=F]
# {do something interesting with f}
f
}
##
R> Foo(DT)
z1 z2
1: 1 2
I think this is an interesting question though - to my knowledge, R doesn't have a concept of something like the this
pointer in C++, but it would certainly be useful in situations like this. Essentially, all of my hackery with sys.call
, get
, etc... was just so I could retrieve the names of the calling object.
这篇关于从括号内反映data.table colulmn名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!