Making an if condition variable ?

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

Making an if condition variable ?

zadig

Dear all,

I want to make an if condition variable like :

a = 10
CONDITION = " a > 0 "

if ( CONDITION ) print(" a is bigger" )

I tried get , getElement , eval without success ?

Thanks

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: Making an if condition variable ?

Jim Lemon-4
Hi ce,

a<-10
condition<-expression("a>0")
if(eval(parse(text=condition))) cat("a>0\n")

Jim

On Thu, Jun 2, 2016 at 12:30 PM, ce <[hidden email]> wrote:

>
> Dear all,
>
> I want to make an if condition variable like :
>
> a = 10
> CONDITION = " a > 0 "
>
> if ( CONDITION ) print(" a is bigger" )
>
> I tried get , getElement , eval without success ?
>
> Thanks
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: Making an if condition variable ?

Ista Zahn
In reply to this post by zadig
if ( eval(parse(text=CONDITION ))) print(" a is bigger" )

Best,
Ista
On Jun 1, 2016 10:32 PM, "ce" <[hidden email]> wrote:

>
> Dear all,
>
> I want to make an if condition variable like :
>
> a = 10
> CONDITION = " a > 0 "
>
> if ( CONDITION ) print(" a is bigger" )
>
> I tried get , getElement , eval without success ?
>
> Thanks
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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: Making an if condition variable ?

Jeff Newmiller
In reply to this post by zadig
Beware of getting too "meta" in your programming... it is rarely worth it. Just write the code and move on with life. That is the beauty of a scripting language.
--
Sent from my phone. Please excuse my brevity.

On June 1, 2016 7:30:29 PM PDT, ce <[hidden email]> wrote:

>
>Dear all,
>
>I want to make an if condition variable like :
>
>a = 10
>CONDITION = " a > 0 "
>
>if ( CONDITION ) print(" a is bigger" )
>
>I tried get , getElement , eval without success ?
>
>Thanks
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>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 -- To UNSUBSCRIBE and more, see
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: Making an if condition variable ?

Richard M. Heiberger
In reply to this post by zadig
a <- 10
CONDITION <-  (a > 0)

if ( CONDITION ) print(" a is bigger" )

On Wed, Jun 1, 2016 at 10:30 PM, ce <[hidden email]> wrote:

>
> Dear all,
>
> I want to make an if condition variable like :
>
> a = 10
> CONDITION = " a > 0 "
>
> if ( CONDITION ) print(" a is bigger" )
>
> I tried get , getElement , eval without success ?
>
> Thanks
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: Making an if condition variable ?

Martin Maechler
In reply to this post by Jim Lemon-4
>>>>> Jim Lemon <[hidden email]>
>>>>>     on Thu, 2 Jun 2016 13:03:01 +1000 writes:

    > Hi ce,

    > a<-10
    > condition<-expression("a>0")
    > if(eval(parse(text=condition))) cat("a>0\n")

While this may answer the question asked,
the above is *not* good advice, excuse me, Jim :

> fortune(106)

If the answer is parse() you should usually rethink the question.
   -- Thomas Lumley
      R-help (February 2005)

> fortune(181)

Personally I have never regretted trying not to underestimate my own future stupidity.
   -- Greg Snow (explaining why eval(parse(...)) is often suboptimal, answering a question
      triggered by the infamous fortune(106))
      R-help (January 2007)

---------------------

Good advice would emphasize to use  expressions rather than
strings.... and yes that's a bit more sophistication.

But it's worth it.
Martin


>
    > Jim

    > On Thu, Jun 2, 2016 at 12:30 PM, ce <[hidden email]> wrote:
    >>
    >> Dear all,
    >>
    >> I want to make an if condition variable like :
    >>
    >> a = 10
    >> CONDITION = " a > 0 "
    >>
    >> if ( CONDITION ) print(" a is bigger" )
    >>
    >> I tried get , getElement , eval without success ?
    >>
    >> Thanks
    >>
    >> ______________________________________________
    >> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
    >> 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.

    > ______________________________________________
    > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
    > 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: Making an if condition variable ?

Rainer Krug-3
In reply to this post by Jeff Newmiller
Jeff Newmiller <[hidden email]> writes:

> Beware of getting too "meta" in your programming... it is rarely worth
> it. Just write the code and move on with life. That is the beauty of a
> scripting language.

+1

I think this a very common pitfall (I know it from own experience...)
and I would say a candidate for a fortune?

Rainer

--
Rainer M. Krug
email: Rainer<at>krugs<dot>de
PGP: 0x0F52F982

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.

signature.asc (463 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Making an if condition variable ?

zadig
In reply to this post by zadig

Thank you all for wisdom :)
Problem is that I change the condition often and then I forget it. I wanted to put it at the beginning of the program with the other parameters so I wouldn't miss it.

ce


-----Original Message-----
From: "Rainer M Krug" [[hidden email]]
Date: 06/02/2016 04:00 AM
To: "Jeff Newmiller" <[hidden email]>
CC: [hidden email]
Subject: Re: Making an if condition variable ?

Jeff Newmiller <[hidden email]> writes:

> Beware of getting too "meta" in your programming... it is rarely worth
> it. Just write the code and move on with life. That is the beauty of a
> scripting language.

+1

I think this a very common pitfall (I know it from own experience...)
and I would say a candidate for a fortune?

Rainer

--
Rainer M. Krug
email: Rainer<at>krugs<dot>de
PGP: 0x0F52F982

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: Making an if condition variable ?

Rainer Krug-3
"ce" <[hidden email]> writes:

> Thank you all for wisdom :)
> Problem is that I change the condition often and then I forget it. I
> wanted to put it at the beginning of the program with the other
> parameters so I wouldn't miss it.

In this case - why not use a function instead of the condition?

cond <- function(x){x>0}
and than

if (cond(4)) {...}

might be the easiest in this case?

or, more flexible,

cond <- function(...){x>0}

if (cond(x=3)) {...}

Cheers,

Rainer

>
> ce
>
>
> -----Original Message-----
> From: "Rainer M Krug" [[hidden email]]
> Date: 06/02/2016 04:00 AM
> To: "Jeff Newmiller" <[hidden email]>
> CC: [hidden email]
> Subject: Re: Making an if condition variable ?
>
> Jeff Newmiller <[hidden email]> writes:
>
>> Beware of getting too "meta" in your programming... it is rarely worth
>> it. Just write the code and move on with life. That is the beauty of a
>> scripting language.
>
> +1
>
> I think this a very common pitfall (I know it from own experience...)
> and I would say a candidate for a fortune?
>
> Rainer
--
Rainer M. Krug
email: Rainer<at>krugs<dot>de
PGP: 0x0F52F982

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.

signature.asc (463 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Making an if condition variable ?

zadig
In reply to this post by zadig

well, not that simple. My original condition and changes are more complicated than I post in r-help for simplicity purposes . Original condition is something like :

 if (   a > 0  & ( b[1] > b[2] ) |  c == TRUE )

and I might change it to

if ( a == 0 &  ( b[1] < b[2] ) & d > 10 )

then I need to have a bunch of functions and I will forget which function I used.

-----Original Message-----
From: "Rainer M Krug" [[hidden email]]
Date: 06/02/2016 09:08 AM
To: "ce" <[hidden email]>
CC: "Jeff Newmiller" <[hidden email]>, [hidden email]
Subject: Re: Making an if condition variable ?

"ce" <[hidden email]> writes:

> Thank you all for wisdom :)
> Problem is that I change the condition often and then I forget it. I
> wanted to put it at the beginning of the program with the other
> parameters so I wouldn't miss it.

In this case - why not use a function instead of the condition?

cond <- function(x){x>0}
and than

if (cond(4)) {...}

might be the easiest in this case?

or, more flexible,

cond <- function(...){x>0}

if (cond(x=3)) {...}

Cheers,

Rainer

>
> ce
>
>
> -----Original Message-----
> From: "Rainer M Krug" [[hidden email]]
> Date: 06/02/2016 04:00 AM
> To: "Jeff Newmiller" <[hidden email]>
> CC: [hidden email]
> Subject: Re: Making an if condition variable ?
>
> Jeff Newmiller <[hidden email]> writes:
>
>> Beware of getting too "meta" in your programming... it is rarely worth
>> it. Just write the code and move on with life. That is the beauty of a
>> scripting language.
>
> +1
>
> I think this a very common pitfall (I know it from own experience...)
> and I would say a candidate for a fortune?
>
> Rainer

--
Rainer M. Krug
email: Rainer<at>krugs<dot>de
PGP: 0x0F52F982

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: Making an if condition variable ?

Bert Gunter-2
You need to rethink. You'll do better in the long -- and probably
short, too -- run working within the language's paradigms rather than
resisting them.

(It's not that R's paradigms are in any sense "the best"; other
languages have different paradigms and you would do better in those
languages with their paradigms rather than with R's).

For example, you could have a single function that returns the
conditional function of your choice by calling the overall function
with an appropriate keyword.  Etc.

Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Jun 2, 2016 at 7:50 AM, ce <[hidden email]> wrote:

>
> well, not that simple. My original condition and changes are more complicated than I post in r-help for simplicity purposes . Original condition is something like :
>
>  if (   a > 0  & ( b[1] > b[2] ) |  c == TRUE )
>
> and I might change it to
>
> if ( a == 0 &  ( b[1] < b[2] ) & d > 10 )
>
> then I need to have a bunch of functions and I will forget which function I used.
>
> -----Original Message-----
> From: "Rainer M Krug" [[hidden email]]
> Date: 06/02/2016 09:08 AM
> To: "ce" <[hidden email]>
> CC: "Jeff Newmiller" <[hidden email]>, [hidden email]
> Subject: Re: Making an if condition variable ?
>
> "ce" <[hidden email]> writes:
>
>> Thank you all for wisdom :)
>> Problem is that I change the condition often and then I forget it. I
>> wanted to put it at the beginning of the program with the other
>> parameters so I wouldn't miss it.
>
> In this case - why not use a function instead of the condition?
>
> cond <- function(x){x>0}
> and than
>
> if (cond(4)) {...}
>
> might be the easiest in this case?
>
> or, more flexible,
>
> cond <- function(...){x>0}
>
> if (cond(x=3)) {...}
>
> Cheers,
>
> Rainer
>
>>
>> ce
>>
>>
>> -----Original Message-----
>> From: "Rainer M Krug" [[hidden email]]
>> Date: 06/02/2016 04:00 AM
>> To: "Jeff Newmiller" <[hidden email]>
>> CC: [hidden email]
>> Subject: Re: Making an if condition variable ?
>>
>> Jeff Newmiller <[hidden email]> writes:
>>
>>> Beware of getting too "meta" in your programming... it is rarely worth
>>> it. Just write the code and move on with life. That is the beauty of a
>>> scripting language.
>>
>> +1
>>
>> I think this a very common pitfall (I know it from own experience...)
>> and I would say a candidate for a fortune?
>>
>> Rainer
>
> --
> Rainer M. Krug
> email: Rainer<at>krugs<dot>de
> PGP: 0x0F52F982
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.