Hi
I would like to have something like
str <- "df$JT == 12"
fun <- function(df) {
b <- eval(parse(str))
return(b)
}
but for performance "eval(parse(a))" should not be evaluated at each
function call, but should work as
fun <- function(df) {
b <- df$JT == 12
return(b)
}
Do you have an idea how I can implement this?
Thx
Christof
If you have a data frame "df" with a column "JT"
Try this one:
str <- "df$JT == 12"
fun<-function(str){b<-eval(parse(text=str))
return(b)}
fun(str)
On Mon, Sep 17, 2012 at 11:57 AM, Christof Kluß
<ckluss@email.uni-kiel.de>wrote:
> Hi
>
> I would like to have something like
>
> str <- "df$JT == 12"
>
> fun <- function(df) {
>
> b <- eval(parse(str))
>
> return(b)
> }
>
> but for performance "eval(parse(a))" should not be evaluated at
each
> function call, but should work as
>
> fun <- function(df) {
>
> b <- df$JT == 12
>
> return(b)
> }
>
> Do you have an idea how I can implement this?
>
> Thx
> Christof
>
> ______________________________________________
> R-help@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.
>
[[alternative HTML version deleted]]
On Sep 16, 2012, at 11:27 PM, Christof Klu? wrote:> Hi > > I would like to have something like > > str <- "df$JT == 12" > > fun <- function(df) { > > b <- eval(parse(str)) > > return(b) > } >What are you trying to do? str <- quote(df$JT == 12)> but for performance "eval(parse(a))" should not be evaluated at each > function call, but should work as > > fun <- function(df) { > > b <- df$JT == 12 > > return(b) > } > > Do you have an idea how I can implement this?Implement what? And what issues regarding "performance" are relevant? What does it mean to "not evaluate at each function call"? str <- quote(df$JT == 12) fun <- function(df) { b<-eval(str) return(b) } df3 <- data.frame(JT=12) fun(df3) #[1] TRUE -- David Winsemius, MD Alameda, CA, USA
On Mon, Sep 17, 2012 at 6:27 PM, Christof Klu? <ckluss at email.uni-kiel.de> wrote:> Hi > > I would like to have something like > > str <- "df$JT == 12" > > fun <- function(df) { > > b <- eval(parse(str)) > > return(b) > } > > but for performance "eval(parse(a))" should not be evaluated at each > function call, but should work as > > fun <- function(df) { > > b <- df$JT == 12 > > return(b) > } > > Do you have an idea how I can implement this?You can do it with bquote()> e<-parse(text="df$str==12")[[1]] > edf$str == 12> bquote(function(df) b<-.(e))function(df) b <- df$str == 12> eval(bquote(function(df) b<-.(e)))function (df) b <- df$str == 12 This saves more time than I expected, about 100ms per evaluation on my computer. -thomas -- Thomas Lumley Professor of Biostatistics University of Auckland