Reputation: 2386
Hoping that someone could post an example of a custom function which acts similar to "." in plyr.
What I have is a data frame. Where I am continuously running queries such as:
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)")
What I would like is to have a function which basically acts as follows:
.(C_1, C_2 + C_3)
Specifically, a vector of formulas which define the attributes I use to select my data. I can treat "+" as OR "*" as AND etc...
I tried looking at the return type for "." from plyr but did not understand it.
Upvotes: 0
Views: 146
Reputation: 2386
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"
Upvotes: 0
Reputation: 179448
A function similar to plyr:::.
is plyr:::.
:
plyr:::.
function (..., .env = parent.frame())
{
structure(as.list(match.call()[-1]), env = .env, class = "quoted")
}
<environment: namespace:plyr>
This returns a list and assigns it a class "quoted". All it does, is to match the arguments of .()
to the column names in the enclosing environment. Try it in a different context:
with(iris, .(Sepal.Length, Species))
List of 2
$ Sepal.Length: symbol Sepal.Length
$ Species : symbol Species
- attr(*, "env")=<environment: 0x2b33598>
- attr(*, "class")= chr "quoted"
What you do with this object next, depends on your purpose. Several methods exist for working with this class:
methods(class="quoted")
[1] as.quoted.quoted* c.quoted* names.quoted* print.quoted* [.quoted*
Non-visible functions are asterisked
So, if you're looking for a function like .()
, perhaps you can simply use .()
Upvotes: 6