need help in if else condition

classic Classic list List threaded Threaded
22 messages Options
12
sss
Reply | Threaded
Open this post in threaded view
|

need help in if else condition

sss
Hello all,
I am trying to run if else condition to alter my data. I have created a
column F with reference to column d  where I want to replace the value to
0 where ever it finds a string or character value like  -,dnr, post and
keep the rest of the rows should have the original value of column d. It is
converting the text to 0 but the numeric values are not the same it is some
value.. will you please help me to figure out why it is happening and what
should be the proper code

here are the code and the output

d=c(1,2,3,"-","dnr","post",10)
e= c(2,2,3,4,5,6,7)
de<- as.data.frame(cbind(d,e))
de$f<-ifelse (de$d=="-"|de$d=="dnr"|de$d=="post",0,de$d)

head(de)

  d e f
1  1 2 2
2  2 2  4
3  3 3 5
4  - 4 0
5  dnr 5 0
6 post 6 0
why the values are changing in  with if else condition


--
Thanks & Regards,
Shubhasmita Sahani

        [[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: need help in if else condition

Eric Berger
Change the definition of de to

de <- data.frame(d,e,stringsAsFactors=FALSE)

Then you should be ok.

Some additional remarks:

1. The ifelse() command is a bit tricky in R. Avoiding it is often a good
policy.
2. I find %in% very useful. You could replace the multiple conditions check
     de$d=="-"|de$d=="dnr"|de$d=="post"
    by
    de$d %in% c("-","dnr","post")

Following the comments above would lead to code such as

de <- data.frame(d,e,stringsAsFactors=FALSE)
de$f <- de$d
de$f[ de$d %in% c("-","dnr","post") ] <- 0

HTH,
Eric






On Wed, Jul 10, 2019 at 10:54 AM Shubhasmita Sahani <
[hidden email]> wrote:

> Hello all,
> I am trying to run if else condition to alter my data. I have created a
> column F with reference to column d  where I want to replace the value to
> 0 where ever it finds a string or character value like  -,dnr, post and
> keep the rest of the rows should have the original value of column d. It is
> converting the text to 0 but the numeric values are not the same it is some
> value.. will you please help me to figure out why it is happening and what
> should be the proper code
>
> here are the code and the output
>
> d=c(1,2,3,"-","dnr","post",10)
> e= c(2,2,3,4,5,6,7)
> de<- as.data.frame(cbind(d,e))
> de$f<-ifelse (de$d=="-"|de$d=="dnr"|de$d=="post",0,de$d)
>
> head(de)
>
>   d e f
> 1  1 2 2
> 2  2 2  4
> 3  3 3 5
> 4  - 4 0
> 5  dnr 5 0
> 6 post 6 0
> why the values are changing in  with if else condition
>
>
> --
> Thanks & Regards,
> Shubhasmita Sahani
>
>         [[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.
>

        [[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.
sss
Reply | Threaded
Open this post in threaded view
|

Re: need help in if else condition

sss
Hello Eric,
Thank you, it worked. both approach.

On Wed, Jul 10, 2019 at 2:09 PM Eric Berger <[hidden email]> wrote:

> Change the definition of de to
>
> de <- data.frame(d,e,stringsAsFactors=FALSE)
>
> Then you should be ok.
>
> Some additional remarks:
>
> 1. The ifelse() command is a bit tricky in R. Avoiding it is often a good
> policy.
> 2. I find %in% very useful. You could replace the multiple conditions check
>      de$d=="-"|de$d=="dnr"|de$d=="post"
>     by
>     de$d %in% c("-","dnr","post")
>
> Following the comments above would lead to code such as
>
> de <- data.frame(d,e,stringsAsFactors=FALSE)
> de$f <- de$d
> de$f[ de$d %in% c("-","dnr","post") ] <- 0
>
> HTH,
> Eric
>
>
>
>
>
>
> On Wed, Jul 10, 2019 at 10:54 AM Shubhasmita Sahani <
> [hidden email]> wrote:
>
>> Hello all,
>> I am trying to run if else condition to alter my data. I have created a
>> column F with reference to column d  where I want to replace the value to
>> 0 where ever it finds a string or character value like  -,dnr, post and
>> keep the rest of the rows should have the original value of column d. It
>> is
>> converting the text to 0 but the numeric values are not the same it is
>> some
>> value.. will you please help me to figure out why it is happening and what
>> should be the proper code
>>
>> here are the code and the output
>>
>> d=c(1,2,3,"-","dnr","post",10)
>> e= c(2,2,3,4,5,6,7)
>> de<- as.data.frame(cbind(d,e))
>> de$f<-ifelse (de$d=="-"|de$d=="dnr"|de$d=="post",0,de$d)
>>
>> head(de)
>>
>>   d e f
>> 1  1 2 2
>> 2  2 2  4
>> 3  3 3 5
>> 4  - 4 0
>> 5  dnr 5 0
>> 6 post 6 0
>> why the values are changing in  with if else condition
>>
>>
>> --
>> Thanks & Regards,
>> Shubhasmita Sahani
>>
>>         [[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.
>>
>

--
Thanks & Regards,
Shubhasmita Sahani

        [[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: need help in if else condition

Richard O'Keefe-2
In reply to this post by sss
Since this has already been answered, I'll just mention one point that was
not addressed.
> d=c(1,2,3,"-","dnr","post",10)
This is rather odd.
> str(d)
 chr [1:7] "1" "2" "3" "-" "dnr" "post" "10"
You can create a vector of logical values, or a vector of numbers, or a
vector of strings,
but if there is a string there, logical values and numbers convert to
strings.
So why not write
> e <- c( 2 , 2 , 3 , 4 ,   5 ,    6 ,  7 )
> d <- c("1","2","3","-","dnr","post","10")
> m <- c("-" = 0, "dnr" = 0, "post" = 0)  # mask unwanted values
> de <- data.frame(d = d, e = e, f = ifelse(is.na(m[d]), d, "0"),
                   stringsAsFactors = FALSE)
> de
     d e  f
1    1 2  1
2    2 2  2
3    3 3  3
4    - 4  0
5  dnr 5  0
6 post 6  0
7   10 7 10

where the numbers in the d and f columns of the result are actually all
strings.


On Wed, 10 Jul 2019 at 19:54, Shubhasmita Sahani <
[hidden email]> wrote:

> Hello all,
> I am trying to run if else condition to alter my data. I have created a
> column F with reference to column d  where I want to replace the value to
> 0 where ever it finds a string or character value like  -,dnr, post and
> keep the rest of the rows should have the original value of column d. It is
> converting the text to 0 but the numeric values are not the same it is some
> value.. will you please help me to figure out why it is happening and what
> should be the proper code
>
> here are the code and the output
>
> d=c(1,2,3,"-","dnr","post",10)
> e= c(2,2,3,4,5,6,7)
> de<- as.data.frame(cbind(d,e))
> de$f<-ifelse (de$d=="-"|de$d=="dnr"|de$d=="post",0,de$d)
>
> head(de)
>
>   d e f
> 1  1 2 2
> 2  2 2  4
> 3  3 3 5
> 4  - 4 0
> 5  dnr 5 0
> 6 post 6 0
> why the values are changing in  with if else condition
>
>
> --
> Thanks & Regards,
> Shubhasmita Sahani
>
>         [[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.
>

        [[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: need help in if else condition

José María Mateos-2
In reply to this post by Eric Berger
On Wed, Jul 10, 2019, at 04:39, Eric Berger wrote:
> 1. The ifelse() command is a bit tricky in R. Avoiding it is often a good
> policy.

You piqued my curiosity, can you elaborate a bit more on this?

--
José María (Chema) Mateos || https://rinzewind.org

______________________________________________
[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: need help in if else condition

Eric Berger
For example, can you predict what the following code will do?
> a <- 1:5
> b <- c(2,3)
> ifelse( a < 3, 1, b)


On Wed, Jul 10, 2019 at 4:34 PM José María Mateos <[hidden email]>
wrote:

> On Wed, Jul 10, 2019, at 04:39, Eric Berger wrote:
> > 1. The ifelse() command is a bit tricky in R. Avoiding it is often a good
> > policy.
>
> You piqued my curiosity, can you elaborate a bit more on this?
>
> --
> José María (Chema) Mateos || https://rinzewind.org
>
> ______________________________________________
> [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: need help in if else condition

Eric Berger
Or even
> a <- 1:5
> [lots of other code]
> x <- ifelse( a < 3, 1, 2)

The point (in this example) is that you might have introduced a bug because
you forgot that 'a' is a vector.
Looking (in isolation) at the assignment to 'x' you believe it's going to
be a single number, either 1 or 2 (unless you know the "shape" of a).
I've been burned by such issues.


On Wed, Jul 10, 2019 at 4:42 PM Eric Berger <[hidden email]> wrote:

> For example, can you predict what the following code will do?
> > a <- 1:5
> > b <- c(2,3)
> > ifelse( a < 3, 1, b)
>
>
> On Wed, Jul 10, 2019 at 4:34 PM José María Mateos <[hidden email]>
> wrote:
>
>> On Wed, Jul 10, 2019, at 04:39, Eric Berger wrote:
>> > 1. The ifelse() command is a bit tricky in R. Avoiding it is often a
>> good
>> > policy.
>>
>> You piqued my curiosity, can you elaborate a bit more on this?
>>
>> --
>> José María (Chema) Mateos || https://rinzewind.org
>>
>> ______________________________________________
>> [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: need help in if else condition

Jeff Newmiller
Sigh. I don't agree with this "avoid ifelse because you might not know the shape of the arguments" advice. All R variables have shapes and sizes, and the better advice is to _pay attention to shapes and sizes_, especially when using vectorized functions like ifelse. An easy way to maintain shape/size consistency is to work with data frames at the top level of your code.

Which is not to say that I use ifelse liberally... it is not the most efficient way to get an answer in many cases. findInterval and cut are often better choices, as is in-place modification:

 x <- rep( 2, length( a ) )
 x[ a < 3 ] <- 1

which can be extended if needed.

On July 10, 2019 6:45:55 AM PDT, Eric Berger <[hidden email]> wrote:

>Or even
>> a <- 1:5
>> [lots of other code]
>> x <- ifelse( a < 3, 1, 2)
>
>The point (in this example) is that you might have introduced a bug
>because
>you forgot that 'a' is a vector.
>Looking (in isolation) at the assignment to 'x' you believe it's going
>to
>be a single number, either 1 or 2 (unless you know the "shape" of a).
>I've been burned by such issues.
>
>
>On Wed, Jul 10, 2019 at 4:42 PM Eric Berger <[hidden email]>
>wrote:
>
>> For example, can you predict what the following code will do?
>> > a <- 1:5
>> > b <- c(2,3)
>> > ifelse( a < 3, 1, b)
>>
>>
>> On Wed, Jul 10, 2019 at 4:34 PM José María Mateos
><[hidden email]>
>> wrote:
>>
>>> On Wed, Jul 10, 2019, at 04:39, Eric Berger wrote:
>>> > 1. The ifelse() command is a bit tricky in R. Avoiding it is often
>a
>>> good
>>> > policy.
>>>
>>> You piqued my curiosity, can you elaborate a bit more on this?
>>>
>>> --
>>> José María (Chema) Mateos || https://rinzewind.org
>>>
>>> ______________________________________________
>>> [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.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[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: need help in if else condition

Richard O'Keefe-2
In reply to this post by Eric Berger
The answer here is that in "ifelse(a < 3, ......)" you ALWAYS expect "a" to
be
a vector because there would be no point in using ifelse if it weren't.
If you believe that "a" is or ought to be a single number, you write
  x <- if (a < 3) 1 else 2
The whole point of ifelse is to vectorise.


On Thu, 11 Jul 2019 at 01:47, Eric Berger <[hidden email]> wrote:

> Or even
> > a <- 1:5
> > [lots of other code]
> > x <- ifelse( a < 3, 1, 2)
>
> The point (in this example) is that you might have introduced a bug because
> you forgot that 'a' is a vector.
> Looking (in isolation) at the assignment to 'x' you believe it's going to
> be a single number, either 1 or 2 (unless you know the "shape" of a).
> I've been burned by such issues.
>
>
> On Wed, Jul 10, 2019 at 4:42 PM Eric Berger <[hidden email]> wrote:
>
> > For example, can you predict what the following code will do?
> > > a <- 1:5
> > > b <- c(2,3)
> > > ifelse( a < 3, 1, b)
> >
> >
> > On Wed, Jul 10, 2019 at 4:34 PM José María Mateos <[hidden email]>
> > wrote:
> >
> >> On Wed, Jul 10, 2019, at 04:39, Eric Berger wrote:
> >> > 1. The ifelse() command is a bit tricky in R. Avoiding it is often a
> >> good
> >> > policy.
> >>
> >> You piqued my curiosity, can you elaborate a bit more on this?
> >>
> >> --
> >> José María (Chema) Mateos || https://rinzewind.org
> >>
> >> ______________________________________________
> >> [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.
>

        [[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: need help in if else condition

Richard O'Keefe-2
In reply to this post by Eric Berger
Expectation: ifelse will use the same "repeat vectors to match the longest"
rule that other vectorised functions do.  So
a <- 1:5
b <- c(2,3)
ifelse(a < 3, 1, b)
=> ifelse(T T F F F <<5>>, 1 <<1>>, 2 3 <<2>>)
=> ifelse(T T F F F <<5>>, 1 1 1 1 1 <<5>>, 2 3 2 3 2 <<5>>)
=> 1 1 2 3 2
and that is indeed the answer you get.  Entirely predictable and consistent
with
other basic operations in R.

The only tricky thing I see is that R has
a strict vectorised  ifelse(logical.vector, some.vector, another.vector)
AND
a non-strict non-vectorised if (logical.scalar) some.value else
another.value
AND
a statement form if (logical.scalar) stmt.1; else stmt.2;


On Thu, 11 Jul 2019 at 01:47, Eric Berger <[hidden email]> wrote:

> For example, can you predict what the following code will do?
> > a <- 1:5
> > b <- c(2,3)
> > ifelse( a < 3, 1, b)
>
>
> On Wed, Jul 10, 2019 at 4:34 PM José María Mateos <[hidden email]>
> wrote:
>
> > On Wed, Jul 10, 2019, at 04:39, Eric Berger wrote:
> > > 1. The ifelse() command is a bit tricky in R. Avoiding it is often a
> good
> > > policy.
> >
> > You piqued my curiosity, can you elaborate a bit more on this?
> >
> > --
> > José María (Chema) Mateos || https://rinzewind.org
> >
> > ______________________________________________
> > [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.
>

        [[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: need help in if else condition

Eric Berger
Of course the behavior of ifelse is predictable.
My point was that for newb's (I was one once) you can get burned if you
don't appreciate that ifelse is vectorized.
Especially if you have some "muscle memory" from using ifelse() in Excel.

On Wed, Jul 10, 2019 at 6:55 PM Richard O'Keefe <[hidden email]> wrote:

> Expectation: ifelse will use the same "repeat vectors to match the longest"
> rule that other vectorised functions do.  So
> a <- 1:5
> b <- c(2,3)
> ifelse(a < 3, 1, b)
> => ifelse(T T F F F <<5>>, 1 <<1>>, 2 3 <<2>>)
> => ifelse(T T F F F <<5>>, 1 1 1 1 1 <<5>>, 2 3 2 3 2 <<5>>)
> => 1 1 2 3 2
> and that is indeed the answer you get.  Entirely predictable and
> consistent with
> other basic operations in R.
>
> The only tricky thing I see is that R has
> a strict vectorised  ifelse(logical.vector, some.vector, another.vector)
> AND
> a non-strict non-vectorised if (logical.scalar) some.value else
> another.value
> AND
> a statement form if (logical.scalar) stmt.1; else stmt.2;
>
>
> On Thu, 11 Jul 2019 at 01:47, Eric Berger <[hidden email]> wrote:
>
>> For example, can you predict what the following code will do?
>> > a <- 1:5
>> > b <- c(2,3)
>> > ifelse( a < 3, 1, b)
>>
>>
>> On Wed, Jul 10, 2019 at 4:34 PM José María Mateos <[hidden email]>
>> wrote:
>>
>> > On Wed, Jul 10, 2019, at 04:39, Eric Berger wrote:
>> > > 1. The ifelse() command is a bit tricky in R. Avoiding it is often a
>> good
>> > > policy.
>> >
>> > You piqued my curiosity, can you elaborate a bit more on this?
>> >
>> > --
>> > José María (Chema) Mateos || https://rinzewind.org
>> >
>> > ______________________________________________
>> > [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.
>>
>

        [[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: need help in if else condition

Dénes Tóth-2
In reply to this post by Richard O'Keefe-2


On 7/10/19 5:54 PM, Richard O'Keefe wrote:

> Expectation: ifelse will use the same "repeat vectors to match the longest"
> rule that other vectorised functions do.  So
> a <- 1:5
> b <- c(2,3)
> ifelse(a < 3, 1, b)
> => ifelse(T T F F F <<5>>, 1 <<1>>, 2 3 <<2>>)
> => ifelse(T T F F F <<5>>, 1 1 1 1 1 <<5>>, 2 3 2 3 2 <<5>>)
> => 1 1 2 3 2
> and that is indeed the answer you get.  Entirely predictable and consistent
> with
> other basic operations in R.
>
> The only tricky thing I see is that R has
> a strict vectorised  ifelse(logical.vector, some.vector, another.vector)
> AND
> a non-strict non-vectorised if (logical.scalar) some.value else
> another.value
> AND
> a statement form if (logical.scalar) stmt.1; else stmt.2;

Just for the records, there is a further form:
`if`(logical.scalar, stmt.1, stmt.2)

The main problem with ifelse is that 1) it is very slow, and 2) the mode
of its return value can be unintuitive or not too predictable (see also
the Value and Warning sections of ?ifelse). One has to be very careful
and ensure that 'yes' and 'no' vectors have the same class, because
ifelse will not warn you at all:
 > ifelse(c(TRUE, TRUE), 1:2, LETTERS[1:2])
[1] 1 2
 > ifelse(c(TRUE, FALSE), 1:2, LETTERS[1:2])
[1] "1" "B"

For options instead of base::ifelse, you might find this discussion helpful:
https://github.com/Rdatatable/data.table/issues/3657


Cheers,
Denes


>
>
> On Thu, 11 Jul 2019 at 01:47, Eric Berger <[hidden email]> wrote:
>
>> For example, can you predict what the following code will do?
>>> a <- 1:5
>>> b <- c(2,3)
>>> ifelse( a < 3, 1, b)
>>
>>
>> On Wed, Jul 10, 2019 at 4:34 PM José María Mateos <[hidden email]>
>> wrote:
>>
>>> On Wed, Jul 10, 2019, at 04:39, Eric Berger wrote:
>>>> 1. The ifelse() command is a bit tricky in R. Avoiding it is often a
>> good
>>>> policy.
>>>
>>> You piqued my curiosity, can you elaborate a bit more on this?
>>>
>>> --
>>> José María (Chema) Mateos || https://rinzewind.org
>>>
>>> ______________________________________________
>>> [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.
>>
>
> [[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.
>

______________________________________________
[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: need help in if else condition

Duncan Murdoch-2
In reply to this post by Richard O'Keefe-2
On 10/07/2019 11:54 a.m., Richard O'Keefe wrote:

> Expectation: ifelse will use the same "repeat vectors to match the longest"
> rule that other vectorised functions do.  So
> a <- 1:5
> b <- c(2,3)
> ifelse(a < 3, 1, b)
> => ifelse(T T F F F <<5>>, 1 <<1>>, 2 3 <<2>>)
> => ifelse(T T F F F <<5>>, 1 1 1 1 1 <<5>>, 2 3 2 3 2 <<5>>)
> => 1 1 2 3 2
> and that is indeed the answer you get.  Entirely predictable and consistent
> with
> other basic operations in R.
>
> The only tricky thing I see is that R has
> a strict vectorised  ifelse(logical.vector, some.vector, another.vector)
> AND
> a non-strict non-vectorised if (logical.scalar) some.value else
> another.value
> AND
> a statement form if (logical.scalar) stmt.1; else stmt.2;
>

That last one is not R syntax. The semicolon in R separates statements,
and "else stmt.2" is not a valid statement.

I'm sure that's just a typo, but I can't think of a variation on it that
is different from the one before.

Duncan Murdoch

______________________________________________
[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: need help in if else condition

Richard O'Keefe-2
In reply to this post by Dénes Tóth-2
"ifelse is very slow"?  Benchmark time.
> x <- runif(1000000)
> y <- runif(1000000)
> system.time(ifelse(x < y, x, y))
   user  system elapsed
  0.403   0.044   0.448
> system.time(y + (x < y)*(x - y))
   user  system elapsed
  0.026   0.012   0.038

This appears to be a quality-of-implementation bug.


On Thu, 11 Jul 2019 at 04:14, Dénes Tóth <[hidden email]> wrote:

>
>
> On 7/10/19 5:54 PM, Richard O'Keefe wrote:
> > Expectation: ifelse will use the same "repeat vectors to match the
> longest"
> > rule that other vectorised functions do.  So
> > a <- 1:5
> > b <- c(2,3)
> > ifelse(a < 3, 1, b)
> > => ifelse(T T F F F <<5>>, 1 <<1>>, 2 3 <<2>>)
> > => ifelse(T T F F F <<5>>, 1 1 1 1 1 <<5>>, 2 3 2 3 2 <<5>>)
> > => 1 1 2 3 2
> > and that is indeed the answer you get.  Entirely predictable and
> consistent
> > with
> > other basic operations in R.
> >
> > The only tricky thing I see is that R has
> > a strict vectorised  ifelse(logical.vector, some.vector, another.vector)
> > AND
> > a non-strict non-vectorised if (logical.scalar) some.value else
> > another.value
> > AND
> > a statement form if (logical.scalar) stmt.1; else stmt.2;
>
> Just for the records, there is a further form:
> `if`(logical.scalar, stmt.1, stmt.2)
>
> The main problem with ifelse is that 1) it is very slow, and 2) the mode
> of its return value can be unintuitive or not too predictable (see also
> the Value and Warning sections of ?ifelse). One has to be very careful
> and ensure that 'yes' and 'no' vectors have the same class, because
> ifelse will not warn you at all:
>  > ifelse(c(TRUE, TRUE), 1:2, LETTERS[1:2])
> [1] 1 2
>  > ifelse(c(TRUE, FALSE), 1:2, LETTERS[1:2])
> [1] "1" "B"
>
> For options instead of base::ifelse, you might find this discussion
> helpful:
> https://github.com/Rdatatable/data.table/issues/3657
>
>
> Cheers,
> Denes
>
>
> >
> >
> > On Thu, 11 Jul 2019 at 01:47, Eric Berger <[hidden email]> wrote:
> >
> >> For example, can you predict what the following code will do?
> >>> a <- 1:5
> >>> b <- c(2,3)
> >>> ifelse( a < 3, 1, b)
> >>
> >>
> >> On Wed, Jul 10, 2019 at 4:34 PM José María Mateos <[hidden email]>
> >> wrote:
> >>
> >>> On Wed, Jul 10, 2019, at 04:39, Eric Berger wrote:
> >>>> 1. The ifelse() command is a bit tricky in R. Avoiding it is often a
> >> good
> >>>> policy.
> >>>
> >>> You piqued my curiosity, can you elaborate a bit more on this?
> >>>
> >>> --
> >>> José María (Chema) Mateos || https://rinzewind.org
> >>>
> >>> ______________________________________________
> >>> [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.
> >>
> >
> >       [[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.
> >
>

        [[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: need help in if else condition

Peter Dalgaard-2
Er, what version is this? I have (on a late 2010 MB Air!)

> system.time(ifelse(x < y, x, y))
   user  system elapsed
  0.072   0.012   0.085

and even

> system.time({r<-numeric(1000000);ix <- x < y; r[ix]<-x[ix]; r[!ix]<-y[!ix]; r})
   user  system elapsed
  0.082   0.053   0.135

-pd


> On 12 Jul 2019, at 15:02 , Richard O'Keefe <[hidden email]> wrote:
>
> "ifelse is very slow"?  Benchmark time.
>> x <- runif(1000000)
>> y <- runif(1000000)
>> system.time(ifelse(x < y, x, y))
>   user  system elapsed
>  0.403   0.044   0.448
>> system.time(y + (x < y)*(x - y))
>   user  system elapsed
>  0.026   0.012   0.038
>
> This appears to be a quality-of-implementation bug.
>
>
> On Thu, 11 Jul 2019 at 04:14, Dénes Tóth <[hidden email]> wrote:
>
>>
>>
>> On 7/10/19 5:54 PM, Richard O'Keefe wrote:
>>> Expectation: ifelse will use the same "repeat vectors to match the
>> longest"
>>> rule that other vectorised functions do.  So
>>> a <- 1:5
>>> b <- c(2,3)
>>> ifelse(a < 3, 1, b)
>>> => ifelse(T T F F F <<5>>, 1 <<1>>, 2 3 <<2>>)
>>> => ifelse(T T F F F <<5>>, 1 1 1 1 1 <<5>>, 2 3 2 3 2 <<5>>)
>>> => 1 1 2 3 2
>>> and that is indeed the answer you get.  Entirely predictable and
>> consistent
>>> with
>>> other basic operations in R.
>>>
>>> The only tricky thing I see is that R has
>>> a strict vectorised  ifelse(logical.vector, some.vector, another.vector)
>>> AND
>>> a non-strict non-vectorised if (logical.scalar) some.value else
>>> another.value
>>> AND
>>> a statement form if (logical.scalar) stmt.1; else stmt.2;
>>
>> Just for the records, there is a further form:
>> `if`(logical.scalar, stmt.1, stmt.2)
>>
>> The main problem with ifelse is that 1) it is very slow, and 2) the mode
>> of its return value can be unintuitive or not too predictable (see also
>> the Value and Warning sections of ?ifelse). One has to be very careful
>> and ensure that 'yes' and 'no' vectors have the same class, because
>> ifelse will not warn you at all:
>>> ifelse(c(TRUE, TRUE), 1:2, LETTERS[1:2])
>> [1] 1 2
>>> ifelse(c(TRUE, FALSE), 1:2, LETTERS[1:2])
>> [1] "1" "B"
>>
>> For options instead of base::ifelse, you might find this discussion
>> helpful:
>> https://github.com/Rdatatable/data.table/issues/3657
>>
>>
>> Cheers,
>> Denes
>>
>>
>>>
>>>
>>> On Thu, 11 Jul 2019 at 01:47, Eric Berger <[hidden email]> wrote:
>>>
>>>> For example, can you predict what the following code will do?
>>>>> a <- 1:5
>>>>> b <- c(2,3)
>>>>> ifelse( a < 3, 1, b)
>>>>
>>>>
>>>> On Wed, Jul 10, 2019 at 4:34 PM José María Mateos <[hidden email]>
>>>> wrote:
>>>>
>>>>> On Wed, Jul 10, 2019, at 04:39, Eric Berger wrote:
>>>>>> 1. The ifelse() command is a bit tricky in R. Avoiding it is often a
>>>> good
>>>>>> policy.
>>>>>
>>>>> You piqued my curiosity, can you elaborate a bit more on this?
>>>>>
>>>>> --
>>>>> José María (Chema) Mateos || https://rinzewind.org
>>>>>
>>>>> ______________________________________________
>>>>> [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.
>>>>
>>>
>>>      [[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.
>>>
>>
>
> [[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.

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[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: need help in if else condition

Peter Dalgaard-2
In reply to this post by José María Mateos-2
One common gotcha is that ifelse() has no way to reconcile attributes between the two alternatives so it takes the attributes for the result from those of the condition, which is almost certainly what you don't want. In particular, this may convert factors to their underlying integer codes.

Another gotcha is that both alternatives are (usually) computed for all indices, whether or not that is sensible. (See, e.g., first example on help page.)

-pd

> On 10 Jul 2019, at 15:33 , José María Mateos <[hidden email]> wrote:
>
> On Wed, Jul 10, 2019, at 04:39, Eric Berger wrote:
>> 1. The ifelse() command is a bit tricky in R. Avoiding it is often a good
>> policy.
>
> You piqued my curiosity, can you elaborate a bit more on this?
>
> --
> José María (Chema) Mateos || https://rinzewind.org
>
> ______________________________________________
> [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.

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[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: need help in if else condition

Richard O'Keefe-2
In reply to this post by Peter Dalgaard-2
Four-core AMD E2-7110 running Ubuntu 18.04 LTS.
The R version is the latest in the repository:
r-base/bionic,bionic,now 3.4.4-1ubuntu1 all [installed]

Why not 3.6?  Because when i followed the installation instructions, adding

deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/

to /etc/apt/sources.list, sudo apt update reported

W: GPG error: https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/
InRelease: The following signatures couldn't be verified because the public
key is not available: NO_PUBKEY 51716619E084DAB9
E: The repository 'https://cloud.r-project.org/bin/linux/ubuntu
bionic-cran35/ InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore
disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration
details.

I just repeated the test.
> x <- runif(1000000)
> y <- runif(1000000)
> system.time(ifelse(x<y,x,y))
   user  system elapsed
  0.359   0.043   0.404
> system.time(pmin(x,y))
   user  system elapsed
  0.015   0.008   0.023
> system.time({r<-numeric(1000000);ix <- x < y; r[ix]<-x[ix];
r[!ix]<-y[!ix]; r})
   user  system elapsed
  0.077   0.028   0.105

On Mon, 15 Jul 2019 at 08:00, peter dalgaard <[hidden email]> wrote:

>
> Er, what version is this? I have (on a late 2010 MB Air!)
>
> > system.time(ifelse(x < y, x, y))
>    user  system elapsed
>   0.072   0.012   0.085
>
> and even
>
> > system.time({r<-numeric(1000000);ix <- x < y; r[ix]<-x[ix];
r[!ix]<-y[!ix]; r})

>    user  system elapsed
>   0.082   0.053   0.135
>
> -pd
>
>
> > On 12 Jul 2019, at 15:02 , Richard O'Keefe <[hidden email]> wrote:
> >
> > "ifelse is very slow"?  Benchmark time.
> >> x <- runif(1000000)
> >> y <- runif(1000000)
> >> system.time(ifelse(x < y, x, y))
> >   user  system elapsed
> >  0.403   0.044   0.448
> >> system.time(y + (x < y)*(x - y))
> >   user  system elapsed
> >  0.026   0.012   0.038
> >
> > This appears to be a quality-of-implementation bug.
> >
> >
> > On Thu, 11 Jul 2019 at 04:14, Dénes Tóth <[hidden email]> wrote:
> >
> >>
> >>
> >> On 7/10/19 5:54 PM, Richard O'Keefe wrote:
> >>> Expectation: ifelse will use the same "repeat vectors to match the
> >> longest"
> >>> rule that other vectorised functions do.  So
> >>> a <- 1:5
> >>> b <- c(2,3)
> >>> ifelse(a < 3, 1, b)
> >>> => ifelse(T T F F F <<5>>, 1 <<1>>, 2 3 <<2>>)
> >>> => ifelse(T T F F F <<5>>, 1 1 1 1 1 <<5>>, 2 3 2 3 2 <<5>>)
> >>> => 1 1 2 3 2
> >>> and that is indeed the answer you get.  Entirely predictable and
> >> consistent
> >>> with
> >>> other basic operations in R.
> >>>
> >>> The only tricky thing I see is that R has
> >>> a strict vectorised  ifelse(logical.vector, some.vector,
another.vector)

> >>> AND
> >>> a non-strict non-vectorised if (logical.scalar) some.value else
> >>> another.value
> >>> AND
> >>> a statement form if (logical.scalar) stmt.1; else stmt.2;
> >>
> >> Just for the records, there is a further form:
> >> `if`(logical.scalar, stmt.1, stmt.2)
> >>
> >> The main problem with ifelse is that 1) it is very slow, and 2) the
mode

> >> of its return value can be unintuitive or not too predictable (see also
> >> the Value and Warning sections of ?ifelse). One has to be very careful
> >> and ensure that 'yes' and 'no' vectors have the same class, because
> >> ifelse will not warn you at all:
> >>> ifelse(c(TRUE, TRUE), 1:2, LETTERS[1:2])
> >> [1] 1 2
> >>> ifelse(c(TRUE, FALSE), 1:2, LETTERS[1:2])
> >> [1] "1" "B"
> >>
> >> For options instead of base::ifelse, you might find this discussion
> >> helpful:
> >> https://github.com/Rdatatable/data.table/issues/3657
> >>
> >>
> >> Cheers,
> >> Denes
> >>
> >>
> >>>
> >>>
> >>> On Thu, 11 Jul 2019 at 01:47, Eric Berger <[hidden email]>
wrote:
> >>>
> >>>> For example, can you predict what the following code will do?
> >>>>> a <- 1:5
> >>>>> b <- c(2,3)
> >>>>> ifelse( a < 3, 1, b)
> >>>>
> >>>>
> >>>> On Wed, Jul 10, 2019 at 4:34 PM José María Mateos <
[hidden email]>
> >>>> wrote:
> >>>>
> >>>>> On Wed, Jul 10, 2019, at 04:39, Eric Berger wrote:
> >>>>>> 1. The ifelse() command is a bit tricky in R. Avoiding it is often
a

> >>>> good
> >>>>>> policy.
> >>>>>
> >>>>> You piqued my curiosity, can you elaborate a bit more on this?
> >>>>>
> >>>>> --
> >>>>> José María (Chema) Mateos || https://rinzewind.org
> >>>>>
> >>>>> ______________________________________________
> >>>>> [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.
> >>>>
> >>>
> >>>      [[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.
> >>>
> >>
> >
> >       [[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.
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: [hidden email]  Priv: [hidden email]
>
>
>
>
>
>
>
>

        [[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: need help in if else condition

Jeff Newmiller
Did you ask for assistance on R-Sig-Debian? you will need to be more explicit than below about what you actually did. FWIW I was able to do it [1]... you might have encountered a temporary network problem.

[1] https://cran.r-project.org/bin/linux/ubuntu/README.html

On July 14, 2019 4:55:25 PM CDT, Richard O'Keefe <[hidden email]> wrote:

>Four-core AMD E2-7110 running Ubuntu 18.04 LTS.
>The R version is the latest in the repository:
>r-base/bionic,bionic,now 3.4.4-1ubuntu1 all [installed]
>
>Why not 3.6?  Because when i followed the installation instructions,
>adding
>
>deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/
>
>to /etc/apt/sources.list, sudo apt update reported
>
>W: GPG error: https://cloud.r-project.org/bin/linux/ubuntu
>bionic-cran35/
>InRelease: The following signatures couldn't be verified because the
>public
>key is not available: NO_PUBKEY 51716619E084DAB9
>E: The repository 'https://cloud.r-project.org/bin/linux/ubuntu
>bionic-cran35/ InRelease' is not signed.
>N: Updating from such a repository can't be done securely, and is
>therefore
>disabled by default.
>N: See apt-secure(8) manpage for repository creation and user
>configuration
>details.
>
>I just repeated the test.
>> x <- runif(1000000)
>> y <- runif(1000000)
>> system.time(ifelse(x<y,x,y))
>   user  system elapsed
>  0.359   0.043   0.404
>> system.time(pmin(x,y))
>   user  system elapsed
>  0.015   0.008   0.023
>> system.time({r<-numeric(1000000);ix <- x < y; r[ix]<-x[ix];
>r[!ix]<-y[!ix]; r})
>   user  system elapsed
>  0.077   0.028   0.105
>
>On Mon, 15 Jul 2019 at 08:00, peter dalgaard <[hidden email]> wrote:
>>
>> Er, what version is this? I have (on a late 2010 MB Air!)
>>
>> > system.time(ifelse(x < y, x, y))
>>    user  system elapsed
>>   0.072   0.012   0.085
>>
>> and even
>>
>> > system.time({r<-numeric(1000000);ix <- x < y; r[ix]<-x[ix];
>r[!ix]<-y[!ix]; r})
>>    user  system elapsed
>>   0.082   0.053   0.135
>>
>> -pd
>>
>>
>> > On 12 Jul 2019, at 15:02 , Richard O'Keefe <[hidden email]>
>wrote:
>> >
>> > "ifelse is very slow"?  Benchmark time.
>> >> x <- runif(1000000)
>> >> y <- runif(1000000)
>> >> system.time(ifelse(x < y, x, y))
>> >   user  system elapsed
>> >  0.403   0.044   0.448
>> >> system.time(y + (x < y)*(x - y))
>> >   user  system elapsed
>> >  0.026   0.012   0.038
>> >
>> > This appears to be a quality-of-implementation bug.
>> >
>> >
>> > On Thu, 11 Jul 2019 at 04:14, Dénes Tóth <[hidden email]>
>wrote:
>> >
>> >>
>> >>
>> >> On 7/10/19 5:54 PM, Richard O'Keefe wrote:
>> >>> Expectation: ifelse will use the same "repeat vectors to match
>the
>> >> longest"
>> >>> rule that other vectorised functions do.  So
>> >>> a <- 1:5
>> >>> b <- c(2,3)
>> >>> ifelse(a < 3, 1, b)
>> >>> => ifelse(T T F F F <<5>>, 1 <<1>>, 2 3 <<2>>)
>> >>> => ifelse(T T F F F <<5>>, 1 1 1 1 1 <<5>>, 2 3 2 3 2 <<5>>)
>> >>> => 1 1 2 3 2
>> >>> and that is indeed the answer you get.  Entirely predictable and
>> >> consistent
>> >>> with
>> >>> other basic operations in R.
>> >>>
>> >>> The only tricky thing I see is that R has
>> >>> a strict vectorised  ifelse(logical.vector, some.vector,
>another.vector)
>> >>> AND
>> >>> a non-strict non-vectorised if (logical.scalar) some.value else
>> >>> another.value
>> >>> AND
>> >>> a statement form if (logical.scalar) stmt.1; else stmt.2;
>> >>
>> >> Just for the records, there is a further form:
>> >> `if`(logical.scalar, stmt.1, stmt.2)
>> >>
>> >> The main problem with ifelse is that 1) it is very slow, and 2)
>the
>mode
>> >> of its return value can be unintuitive or not too predictable (see
>also
>> >> the Value and Warning sections of ?ifelse). One has to be very
>careful
>> >> and ensure that 'yes' and 'no' vectors have the same class,
>because
>> >> ifelse will not warn you at all:
>> >>> ifelse(c(TRUE, TRUE), 1:2, LETTERS[1:2])
>> >> [1] 1 2
>> >>> ifelse(c(TRUE, FALSE), 1:2, LETTERS[1:2])
>> >> [1] "1" "B"
>> >>
>> >> For options instead of base::ifelse, you might find this
>discussion
>> >> helpful:
>> >> https://github.com/Rdatatable/data.table/issues/3657
>> >>
>> >>
>> >> Cheers,
>> >> Denes
>> >>
>> >>
>> >>>
>> >>>
>> >>> On Thu, 11 Jul 2019 at 01:47, Eric Berger <[hidden email]>
>wrote:
>> >>>
>> >>>> For example, can you predict what the following code will do?
>> >>>>> a <- 1:5
>> >>>>> b <- c(2,3)
>> >>>>> ifelse( a < 3, 1, b)
>> >>>>
>> >>>>
>> >>>> On Wed, Jul 10, 2019 at 4:34 PM José María Mateos <
>[hidden email]>
>> >>>> wrote:
>> >>>>
>> >>>>> On Wed, Jul 10, 2019, at 04:39, Eric Berger wrote:
>> >>>>>> 1. The ifelse() command is a bit tricky in R. Avoiding it is
>often
>a
>> >>>> good
>> >>>>>> policy.
>> >>>>>
>> >>>>> You piqued my curiosity, can you elaborate a bit more on this?
>> >>>>>
>> >>>>> --
>> >>>>> José María (Chema) Mateos || https://rinzewind.org
>> >>>>>
>> >>>>> ______________________________________________
>> >>>>> [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.
>> >>>>
>> >>>
>> >>>      [[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.
>> >>>
>> >>
>> >
>> >       [[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.
>>
>> --
>> Peter Dalgaard, Professor,
>> Center for Statistics, Copenhagen Business School
>> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
>> Phone: (+45)38153501
>> Office: A 4.23
>> Email: [hidden email]  Priv: [hidden email]
>>
>>
>>
>>
>>
>>
>>
>>
>
> [[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.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[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: need help in if else condition

Richard O'Keefe-2
I didn't communicate my problem to R-Sig-Debian because I had never
previously
heard of them.  Thank you for the tip.
Poking through the recent archives there I see other people
have had (different) trouble with the Bionic repository.

Fortunately, the instructions at
https://linuxize.com/post/how-to-install-r-on-ubuntu-18-04/
worked, after I first removed the existing installation of R.
All I had to do after that was reinstall about 180 packages.
Is there a way to do this automatically?

However, having installed "Action of the Toes" (are R releases named
by Culture AIs, by any chance?) I still find
> x <- runif(1000000)
> y <- runif(1000000)
> system.time(ifelse(x < y, x, y))
   user  system elapsed
  0.087   0.047   0.135
> system.time(y + (x < y)*(x-y))
   user  system elapsed
  0.035   0.008   0.042
> system.time(pmin(x,y))
   user  system elapsed
  0.025   0.004   0.030

On Mon, 15 Jul 2019 at 12:04, Jeff Newmiller <[hidden email]>
wrote:

> Did you ask for assistance on R-Sig-Debian? you will need to be more
> explicit than below about what you actually did. FWIW I was able to do it
> [1]... you might have encountered a temporary network problem.
>
> [1] https://cran.r-project.org/bin/linux/ubuntu/README.html
>
> On July 14, 2019 4:55:25 PM CDT, Richard O'Keefe <[hidden email]> wrote:
> >Four-core AMD E2-7110 running Ubuntu 18.04 LTS.
> >The R version is the latest in the repository:
> >r-base/bionic,bionic,now 3.4.4-1ubuntu1 all [installed]
> >
> >Why not 3.6?  Because when i followed the installation instructions,
> >adding
> >
> >deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/
> >
> >to /etc/apt/sources.list, sudo apt update reported
> >
> >W: GPG error: https://cloud.r-project.org/bin/linux/ubuntu
> >bionic-cran35/
> >InRelease: The following signatures couldn't be verified because the
> >public
> >key is not available: NO_PUBKEY 51716619E084DAB9
> >E: The repository 'https://cloud.r-project.org/bin/linux/ubuntu
> >bionic-cran35/ InRelease' is not signed.
> >N: Updating from such a repository can't be done securely, and is
> >therefore
> >disabled by default.
> >N: See apt-secure(8) manpage for repository creation and user
> >configuration
> >details.
> >
> >I just repeated the test.
> >> x <- runif(1000000)
> >> y <- runif(1000000)
> >> system.time(ifelse(x<y,x,y))
> >   user  system elapsed
> >  0.359   0.043   0.404
> >> system.time(pmin(x,y))
> >   user  system elapsed
> >  0.015   0.008   0.023
> >> system.time({r<-numeric(1000000);ix <- x < y; r[ix]<-x[ix];
> >r[!ix]<-y[!ix]; r})
> >   user  system elapsed
> >  0.077   0.028   0.105
> >
> >On Mon, 15 Jul 2019 at 08:00, peter dalgaard <[hidden email]> wrote:
> >>
> >> Er, what version is this? I have (on a late 2010 MB Air!)
> >>
> >> > system.time(ifelse(x < y, x, y))
> >>    user  system elapsed
> >>   0.072   0.012   0.085
> >>
> >> and even
> >>
> >> > system.time({r<-numeric(1000000);ix <- x < y; r[ix]<-x[ix];
> >r[!ix]<-y[!ix]; r})
> >>    user  system elapsed
> >>   0.082   0.053   0.135
> >>
> >> -pd
> >>
> >>
> >> > On 12 Jul 2019, at 15:02 , Richard O'Keefe <[hidden email]>
> >wrote:
> >> >
> >> > "ifelse is very slow"?  Benchmark time.
> >> >> x <- runif(1000000)
> >> >> y <- runif(1000000)
> >> >> system.time(ifelse(x < y, x, y))
> >> >   user  system elapsed
> >> >  0.403   0.044   0.448
> >> >> system.time(y + (x < y)*(x - y))
> >> >   user  system elapsed
> >> >  0.026   0.012   0.038
> >> >
> >> > This appears to be a quality-of-implementation bug.
> >> >
> >> >
> >> > On Thu, 11 Jul 2019 at 04:14, Dénes Tóth <[hidden email]>
> >wrote:
> >> >
> >> >>
> >> >>
> >> >> On 7/10/19 5:54 PM, Richard O'Keefe wrote:
> >> >>> Expectation: ifelse will use the same "repeat vectors to match
> >the
> >> >> longest"
> >> >>> rule that other vectorised functions do.  So
> >> >>> a <- 1:5
> >> >>> b <- c(2,3)
> >> >>> ifelse(a < 3, 1, b)
> >> >>> => ifelse(T T F F F <<5>>, 1 <<1>>, 2 3 <<2>>)
> >> >>> => ifelse(T T F F F <<5>>, 1 1 1 1 1 <<5>>, 2 3 2 3 2 <<5>>)
> >> >>> => 1 1 2 3 2
> >> >>> and that is indeed the answer you get.  Entirely predictable and
> >> >> consistent
> >> >>> with
> >> >>> other basic operations in R.
> >> >>>
> >> >>> The only tricky thing I see is that R has
> >> >>> a strict vectorised  ifelse(logical.vector, some.vector,
> >another.vector)
> >> >>> AND
> >> >>> a non-strict non-vectorised if (logical.scalar) some.value else
> >> >>> another.value
> >> >>> AND
> >> >>> a statement form if (logical.scalar) stmt.1; else stmt.2;
> >> >>
> >> >> Just for the records, there is a further form:
> >> >> `if`(logical.scalar, stmt.1, stmt.2)
> >> >>
> >> >> The main problem with ifelse is that 1) it is very slow, and 2)
> >the
> >mode
> >> >> of its return value can be unintuitive or not too predictable (see
> >also
> >> >> the Value and Warning sections of ?ifelse). One has to be very
> >careful
> >> >> and ensure that 'yes' and 'no' vectors have the same class,
> >because
> >> >> ifelse will not warn you at all:
> >> >>> ifelse(c(TRUE, TRUE), 1:2, LETTERS[1:2])
> >> >> [1] 1 2
> >> >>> ifelse(c(TRUE, FALSE), 1:2, LETTERS[1:2])
> >> >> [1] "1" "B"
> >> >>
> >> >> For options instead of base::ifelse, you might find this
> >discussion
> >> >> helpful:
> >> >> https://github.com/Rdatatable/data.table/issues/3657
> >> >>
> >> >>
> >> >> Cheers,
> >> >> Denes
> >> >>
> >> >>
> >> >>>
> >> >>>
> >> >>> On Thu, 11 Jul 2019 at 01:47, Eric Berger <[hidden email]>
> >wrote:
> >> >>>
> >> >>>> For example, can you predict what the following code will do?
> >> >>>>> a <- 1:5
> >> >>>>> b <- c(2,3)
> >> >>>>> ifelse( a < 3, 1, b)
> >> >>>>
> >> >>>>
> >> >>>> On Wed, Jul 10, 2019 at 4:34 PM José María Mateos <
> >[hidden email]>
> >> >>>> wrote:
> >> >>>>
> >> >>>>> On Wed, Jul 10, 2019, at 04:39, Eric Berger wrote:
> >> >>>>>> 1. The ifelse() command is a bit tricky in R. Avoiding it is
> >often
> >a
> >> >>>> good
> >> >>>>>> policy.
> >> >>>>>
> >> >>>>> You piqued my curiosity, can you elaborate a bit more on this?
> >> >>>>>
> >> >>>>> --
> >> >>>>> José María (Chema) Mateos || https://rinzewind.org
> >> >>>>>
> >> >>>>> ______________________________________________
> >> >>>>> [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.
> >> >>>>
> >> >>>
> >> >>>      [[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.
> >> >>>
> >> >>
> >> >
> >> >       [[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.
> >>
> >> --
> >> Peter Dalgaard, Professor,
> >> Center for Statistics, Copenhagen Business School
> >> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> >> Phone: (+45)38153501
> >> Office: A 4.23
> >> Email: [hidden email]  Priv: [hidden email]
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >
> >       [[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.
>
> --
> Sent from my phone. Please excuse my brevity.
>

        [[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: need help in if else condition

S Ellison-2
> However, having installed "Action of the Toes" (are R releases named
> by Culture AIs, by any chance?)

Peanuts cartoons.

https://twitter.com/snoopy/status/269963551135891456

Steve E


*******************************************************************
This email and any attachments are confidential. Any use, copying or
disclosure other than by the intended recipient is unauthorised. If
you have received this message in error, please notify the sender
immediately via +44(0)20 8943 7000 or notify [hidden email]
and delete this message and any copies from your computer and network.
LGC Limited. Registered in England 2991879.
Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK
______________________________________________
[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.
12