2012-05-09 29 views
0

希望有人可以發佈一個類似於「。」的自定義函數的示例。在plyr。創建與「。」類似的功能。來自Plyr

我有一個數據框。當我連續運行的查詢,如:

sqldf("select * from event.df where Date in (select Date from condition.df where C_1 = 1 and (C_2 = 1 OR C_3 = 3)") 

我想什麼是有其基本作用的功能如下:

.(C_1, C_2 + C_3) 

具體來說,它定義我使用的屬性公式的矢量選擇我的數據。我可以把「+」當作或者「*」作爲AND等等......

我試着看着「。」的返回類型。來自plyr但並不理解它。

回答

6

類似plyr:::.函數是plyr:::.

plyr:::. 
function (..., .env = parent.frame()) 
{ 
    structure(as.list(match.call()[-1]), env = .env, class = "quoted") 
} 
<environment: namespace:plyr> 

這將返回一個列表,並賦予它一個類「援引」。它所做的只是將.()的參數與封閉環境中的列名相匹配。在不同的上下文中嘗試它:

with(iris, .(Sepal.Length, Species)) 
List of 2 
$ Sepal.Length: symbol Sepal.Length 
$ Species  : symbol Species 
- attr(*, "env")=<environment: 0x2b33598> 
- attr(*, "class")= chr "quoted" 

接下來你用這個對象做什麼取決於你的目的。幾種方法與此類合作存在:

methods(class="quoted") 
[1] as.quoted.quoted* c.quoted*   names.quoted*  print.quoted*  [.quoted*   

    Non-visible functions are asterisked 

所以,如果你正在尋找像.()的功能,也許你可以簡單地使用.()

+0

名稱實際上可能幫助! (a + b)是否僅僅對除「+」以外的所有標記進行字符串拆分? – Dave

+0

通過將列匹配到封閉環境,你的意思是什麼? – Dave

0
parse <- function (formula, blank.char = ".") 
{ 
    formula <- paste(names(formula), collapse = "") 
    vars <- function(x) { 
     if (is.na(x)) 
      return(NULL) 
     remove.blank(strsplit(gsub("\\s+", "", x), "[*+]")[[1]]) 
    } 
    remove.blank <- function(x) { 
     x <- x[x != blank.char] 
     if (length(x) == 0) 
      NULL 
     else x 
    } 
    parts <- strsplit(formula, "\\|")[[1]] 
    list(m = lapply(strsplit(parts[1], "~")[[1]], vars), l = lapply(strsplit(parts[2], "~")[[1]], vars)) 
} 

parse(.(a + b ~ c + d | e)) 

    $m 
$m[[1]] 
[1] "a" "b" 

$m[[2]] 
[1] "c" "d" 


$l 
$l[[1]] 
[1] "e" 
+2

你可能不想覆蓋'base ::: parse' – Gregor