using a condition given as string in subset function - how?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

using a condition given as string in subset function - how?

markheckmann
df <- data.frame(A=c(1,2), B=c(1,1))

I have a string containing a condition for a subset function, like:
conditionAsString <- paste(names(df), df[1,], sep="==", collapse=" & ")
 > conditionAsString
 > "A==1 & B==1"

Now I want to use this string in the subset call, like

subset(df, conditionAsString)

I do not exactly now how to combine substitute, expression, parse and  
so on to get what I want, which would be:

subset(df, A==1 & B==1)

but using the string conditionAsString.

Thanks,
Mark
–––––––––––––––––––––––––––––––––––––––
Mark Heckmann
Blog: www.markheckmann.de
R-Blog: http://ryouready.wordpress.com





        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: using a condition given as string in subset function - how?

jholtman
I know that if you have to resort to 'parse(text=...)', you should look for
another way (it is a 'fortune'), but it is getting late, and at least it
works:

> eval(parse(text="subset(df, A==1 & B==1)"))
  A B
1 1 1


On Sat, Mar 20, 2010 at 9:09 PM, Mark Heckmann <[hidden email]> wrote:

> df <- data.frame(A=c(1,2), B=c(1,1))
>
> I have a string containing a condition for a subset function, like:
> conditionAsString <- paste(names(df), df[1,], sep="==", collapse=" & ")
>  > conditionAsString
>  > "A==1 & B==1"
>
> Now I want to use this string in the subset call, like
>
> subset(df, conditionAsString)
>
> I do not exactly now how to combine substitute, expression, parse and
> so on to get what I want, which would be:
>
> subset(df, A==1 & B==1)
>
> but using the string conditionAsString.
>
> Thanks,
> Mark
> –––––––––––––––––––––––––––––––––––––––
> Mark Heckmann
> Blog: www.markheckmann.de
> R-Blog: http://ryouready.wordpress.com
>
>
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
>

--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: using a condition given as string in subset function - how?

Albert-Jan Roskam
Hi,

Is eval always used in conjunction with parse? Based on other languages, I'd expect the expression already to work without the use of parse(), but indeed it doesn't, or at least not as intended. Just a newbie question..

Cheers!!

Albert-Jan



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In the face of ambiguity, refuse the temptation to guess.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Sun, 3/21/10, jim holtman <[hidden email]> wrote:

From: jim holtman <[hidden email]>
Subject: Re: [R] using a condition given as string in subset function - how?
To: "Mark Heckmann" <[hidden email]>
Cc: [hidden email]
Date: Sunday, March 21, 2010, 2:33 AM

I know that if you have to resort to 'parse(text=...)', you should look for
another way (it is a 'fortune'), but it is getting late, and at least it
works:

> eval(parse(text="subset(df, A==1 & B==1)"))
  A B
1 1 1


On Sat, Mar 20, 2010 at 9:09 PM, Mark Heckmann <[hidden email]> wrote:

> df <- data.frame(A=c(1,2), B=c(1,1))
>
> I have a string containing a condition for a subset function, like:
> conditionAsString <- paste(names(df), df[1,], sep="==", collapse=" & ")
>  > conditionAsString
>  > "A==1 & B==1"
>
> Now I want to use this string in the subset call, like
>
> subset(df, conditionAsString)
>
> I do not exactly now how to combine substitute, expression, parse and
> so on to get what I want, which would be:
>
> subset(df, A==1 & B==1)
>
> but using the string conditionAsString.
>
> Thanks,
> Mark
> –––––––––––––––––––––––––––––––––––––––
> Mark Heckmann
> Blog: www.markheckmann.de
> R-Blog: http://ryouready.wordpress.com
>
>
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
>

--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

    [[alternative HTML version deleted]]


-----Inline Attachment Follows-----

______________________________________________
[hidden email] 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]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: using a condition given as string in subset function - how?

Thomas Lumley
On Sun, 21 Mar 2010, Albert-Jan Roskam wrote:

> Hi,
>
> Is eval always used in conjunction with parse? Based on other languages, I'd expect the expression already to work without the use of parse(), but indeed it doesn't, or at least not as intended. Just a newbie question..

parse() turns a character string into R code, and eval() evaluates R code.

parse() usually requires eval(), because there's rarely much point parsing a string if you aren't going to evaluate it, but eval() usually doesn't require parse().  That is, most uses of eval() are not on strings but on expressions.

In your case it is not clear whether someone else hands you a condition in string form, in which care parse() is necessary, or whether you build it up your self, in which case parse() is not necessary, and eval() may not be necessary.

         -thomas


> Cheers!!
>
> Albert-Jan
>
>
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> In the face of ambiguity, refuse the temptation to guess.
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> --- On Sun, 3/21/10, jim holtman <[hidden email]> wrote:
>
> From: jim holtman <[hidden email]>
> Subject: Re: [R] using a condition given as string in subset function - how?
> To: "Mark Heckmann" <[hidden email]>
> Cc: [hidden email]
> Date: Sunday, March 21, 2010, 2:33 AM
>
> I know that if you have to resort to 'parse(text=...)', you should look for
> another way (it is a 'fortune'), but it is getting late, and at least it
> works:
>
>> eval(parse(text="subset(df, A==1 & B==1)"))
> ?? A B
> 1 1 1
>
>
> On Sat, Mar 20, 2010 at 9:09 PM, Mark Heckmann <[hidden email]> wrote:
>
>> df <- data.frame(A=c(1,2), B=c(1,1))
>>
>> I have a string containing a condition for a subset function, like:
>> conditionAsString <- paste(names(df), df[1,], sep="==", collapse=" & ")
>> ?? > conditionAsString
>> ?? > "A==1 & B==1"
>>
>> Now I want to use this string in the subset call, like
>>
>> subset(df, conditionAsString)
>>
>> I do not exactly now how to combine substitute, expression, parse and
>> so on to get what I want, which would be:
>>
>> subset(df, A==1 & B==1)
>>
>> but using the string conditionAsString.
>>
>> Thanks,
>> Mark
>> ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
>> Mark Heckmann
>> Blog: www.markheckmann.de
>> R-Blog: http://ryouready.wordpress.com
>>
>>
>>
>>
>>
>> ?? ?? ?? ?? [[alternative HTML version deleted]]
>>
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem that you are trying to solve?
>
> ?????? [[alternative HTML version deleted]]
>
>
> -----Inline Attachment Follows-----
>
> ______________________________________________
> [hidden email] 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]]
>
>

Thomas Lumley Assoc. Professor, Biostatistics
[hidden email] University of Washington, Seattle

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: using a condition given as string in subset function - how?

William Dunlap
In reply to this post by Albert-Jan Roskam
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Albert-Jan Roskam
> Sent: Sunday, March 21, 2010 2:25 AM
> To: Mark Heckmann; jim holtman
> Cc: [hidden email]
> Subject: Re: [R] using a condition given as string in subset
> function - how?
>
> Hi,
>
> Is eval always used in conjunction with parse? Based on other
> languages, I'd expect the expression already to work without
> the use of parse(), but indeed it doesn't, or at least not as
> intended. Just a newbie question..

When eval is given a parse tree (e.g., the output of
parse() or call()) it recursively ascends the tree,
effectively calling eval() on the subtrees.  E.g.,
parse(text='paste("No", 1)')[[1]] returns a "call" object
of length 3, the elements being the name object `paste`,
the character object "No", and the numeric object 1.
Evaluating a name object returns its value and evaluating
a character or numeric object returns the object (unchanged).
I.e., in this case
   eval(`paste`) -> function(..., sep=" ", collapse=NULL)
   eval("No") -> "No"
   eval(1) -> 1
The root of the parse tree is the call object and evaluating
that means applying the function given by the first argument
to the rest of the argumeents.

If eval("No") meant the same as eval(parse(text="No"))
then this scheme would break down, since you wouldn't be able
to distinguish between character objects and name objects,
so it would not be possible to say you wanted the string "No"
or the value of the object called "No".
 
You might say that eval("String") should do one thing when
called directly, whatever that means, and another when called
by a recursive call to eval(), but I think functions should
act the same no matter who calls them.

There are also times when you want the raw parse tree, as when
processing model formulae like log(response)~groupId/predictor
or trellis/lattice formulae like log(y)~predictor|groupId.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

>
> Cheers!!
>
> Albert-Jan
>
>
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> In the face of ambiguity, refuse the temptation to guess.
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> --- On Sun, 3/21/10, jim holtman <[hidden email]> wrote:
>
> From: jim holtman <[hidden email]>
> Subject: Re: [R] using a condition given as string in subset
> function - how?
> To: "Mark Heckmann" <[hidden email]>
> Cc: [hidden email]
> Date: Sunday, March 21, 2010, 2:33 AM
>
> I know that if you have to resort to 'parse(text=...)', you
> should look for
> another way (it is a 'fortune'), but it is getting late, and
> at least it
> works:
>
> > eval(parse(text="subset(df, A==1 & B==1)"))
>   A B
> 1 1 1
>
>
> On Sat, Mar 20, 2010 at 9:09 PM, Mark Heckmann
> <[hidden email]> wrote:
>
> > df <- data.frame(A=c(1,2), B=c(1,1))
> >
> > I have a string containing a condition for a subset function, like:
> > conditionAsString <- paste(names(df), df[1,], sep="==",
> collapse=" & ")
> >  > conditionAsString
> >  > "A==1 & B==1"
> >
> > Now I want to use this string in the subset call, like
> >
> > subset(df, conditionAsString)
> >
> > I do not exactly now how to combine substitute, expression,
> parse and
> > so on to get what I want, which would be:
> >
> > subset(df, A==1 & B==1)
> >
> > but using the string conditionAsString.
> >
> > Thanks,
> > Mark
> >
> ––––––––––––––––––––â€
“––––––––––––––––––

> > Mark Heckmann
> > Blog: www.markheckmann.de
> > R-Blog: http://ryouready.wordpress.com
> >
> >
> >
> >
> >
> >        [[alternative HTML version deleted]]
> >
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> >
> http://www.R-project.org/posting-guide.html<http://www.r-proje
ct.org/posting-guide.html>

> > and provide commented, minimal, self-contained, reproducible code.
> >
> >
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem that you are trying to solve?
>
>     [[alternative HTML version deleted]]
>
>
> -----Inline Attachment Follows-----
>
> ______________________________________________
> [hidden email] 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]]
>
>
______________________________________________
[hidden email] 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.