當然還有一個更慣用的做法,但是這是我想出了:
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
我不得不在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不具有像the this
pointer in C++一個概念,但肯定會是有用的,像這樣的情況。從本質上講,我所有的sys.call
,get
等hackery都只是讓我可以檢索調用對象的名稱。
也許data.table開發者可以添加一個'.COLNAMES'對象(比如'.I','.BY'等),以便在括號內用於這樣的事情。就我個人而言,我只會做兩行這樣的操作。 – Frank 2015-04-03 17:01:54