Hello R-helpers,
I'm writing a long function in which I manipulate a certain number of
datasets. I want the arguments of said function to allow me to adapt the way I
do this. Among other things, I want my function to have an argument which I will
pass on to subset() somewhere inside my function. Here is a quick and simplified
example with the iris dataset.
myfunction<-function(table, extraction) {
table2<-subset(table, extraction)
return(table2) }
myfunction(iris, extraction= Species=="setosa")
############## end
What I would like is for this function to return exactly the same thing as :
subset(iris, Species=="setosa")
Thanks for your help.
Regards,
David Gouache
Available free for the typing are the functions for the default and
the dataframe methods of subset:
> subset.default
function (x, subset, ...)
{
if (!is.logical(subset))
stop("'subset' must be logical")
x[subset & !is.na(subset)]
}
> subset.data.frame
function (x, subset, select, drop = FALSE, ...)
{
if (missing(subset))
r <- TRUE
else {
e <- substitute(subset)
r <- eval(e, x, parent.frame())
if (!is.logical(r))
stop("'subset' must evaluate to logical")
r <- r & !is.na(r)
}
if (missing(select))
vars <- TRUE
else {
nl <- as.list(1:ncol(x))
names(nl) <- names(x)
vars <- eval(substitute(select), nl, parent.frame())
}
x[r, vars, drop = drop]
}
(There is also a matrix method.)
--
David Winsemius
On Dec 17, 2008, at 2:07 PM, GOUACHE David wrote:
> Hello R-helpers,
>
> I'm writing a long function in which I manipulate a certain number
> of datasets. I want the arguments of said function to allow me to
> adapt the way I do this. Among other things, I want my function to
> have an argument which I will pass on to subset() somewhere inside
> my function. Here is a quick and simplified example with the iris
> dataset.
>
> myfunction<-function(table, extraction) {
> table2<-subset(table, extraction)
> return(table2) }
>
> myfunction(iris, extraction= Species=="setosa")
>
>
> ############## end
>
> What I would like is for this function to return exactly the same
> thing as :
> subset(iris, Species=="setosa")
>
>
> Thanks for your help.
>
> Regards,
>
> David Gouache
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
I wrote a dirty hack last time I faced this problem, I'll be curious
to see what is the proper way of dealing with the scoping and
evaluation rules.
library(datasets)
myfunction<-function(table, extraction) {
table2<-subset(table,extraction)
return(table2)
}
condition1 <- quote(iris$Species=="setosa") # I'm not sure how
to
evaluate within the environment, perhaps you could use with(table,
subset(table, extraction) ) in your function
condition2 <- bquote(.(condition1) & iris$Sepal.Width < 3.5) # say,
if you want to concatenate several conditions together
myfunction(iris, extraction= eval(condition1))
myfunction(iris, extraction= eval(condition2))
Best wishes,
baptiste
On 17 Dec 2008, at 19:07, GOUACHE David wrote:
> Hello R-helpers,
>
> I'm writing a long function in which I manipulate a certain number
> of datasets. I want the arguments of said function to allow me to
> adapt the way I do this. Among other things, I want my function to
> have an argument which I will pass on to subset() somewhere inside
> my function. Here is a quick and simplified example with the iris
> dataset.
>
> myfunction<-function(table, extraction) {
> table2<-subset(table, extraction)
> return(table2) }
>
> myfunction(iris, extraction= Species=="setosa")
>
>
> ############## end
>
> What I would like is for this function to return exactly the same
> thing as :
> subset(iris, Species=="setosa")
>
>
> Thanks for your help.
>
> Regards,
>
> David Gouache
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
_____________________________
Baptiste Augui?
School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK
Phone: +44 1392 264187
http://newton.ex.ac.uk/research/emag
This thread may help? https://stat.ethz.ch/pipermail/r-help/2007-November/145345.html On Wed, 17 Dec 2008 20:07:08 +0100, "GOUACHE David" <D.GOUACHE at arvalisinstitutduvegetal.fr> wrote:> Hello R-helpers, > > I'm writing a long function in which I manipulate a certain number of > datasets. I want the arguments of said function to allow me to adapt the > way I do this. Among other things, I want my function to have an argument > which I will pass on to subset() somewhere inside my function. Here is a > quick and simplified example with the iris dataset. > > myfunction<-function(table, extraction) { > table2<-subset(table, extraction) > return(table2) } > > myfunction(iris, extraction= Species=="setosa") > > > ############## end > > What I would like is for this function to return exactly the same thingas> : > subset(iris, Species=="setosa") > > > Thanks for your help. > > Regards, > > David Gouache > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
On Wed, 17 Dec 2008 20:07:08 +0100, GOUACHE David <D.GOUACHE at arvalisinstitutduvegetal.fr> wrote:> argument which I will pass on to subset() somewhere inside my function.I would use the example of .() function from plyr package in this case: .<-function (...){ structure(as.list(match.call()[-1]), class = "quoted") } myfunction<-function(table, extraction) { table2<-subset(table, eval(extraction[[1]])) return(table2) } myfunction(iris, extraction = .(Species=="setosa")) You can pass as many arguments in .() as you wish and index correspondingly in myfunction afterwards. HTH.