14

I'm wondering why ifelse(1<2,print("true"),print("false")) returns

[1] "true"
[1] "true"

whereas ifelse(1<2,"true","false") returns

[1] "true"

I don't understand why the print within ifelse returns "true" twice

rawr
  • 20,481
  • 4
  • 44
  • 78
Gauthaman
  • 139
  • 1
  • 1
  • 4
  • 3
    Probably related to [this](http://stackoverflow.com/questions/16275149/does-ifelse-really-calculate-both-of-its-vectors-every-time-is-it-slow/16275201#16275201). – MichaelChirico Jul 25 '15 at 20:11
  • 1
    also note that `ifelse` is really designed to be applied to vectors, not scalars, so perhaps you're better off using `if (1<2) print("true") else print("false")` anyway. – MichaelChirico Jul 25 '15 at 20:15
  • 3
    And note that if you assign it, like `z <- ifelse(1 < 2, print("true"), print("false"))`, the new object will hold the correct result. – ulfelder Jul 25 '15 at 20:17
  • An ancient tale says that every time a non-programmer dude says the word **bug** a programmer die. You have to clap your hands to make him live again. – SabDeM Jul 25 '15 at 21:29
  • 1
    `ifelse(1 < 2, print(print("true")), print("false"))` is another way to get a good look at what is happening. Then reverse the operator. – Rich Scriven Jul 25 '15 at 21:30

3 Answers3

14

This happens because ifelse will always return a value. When you run ifelse(1<2,print("true"),print("false")), your yes condition is chosen. This condition is a function call to print "true" on the console, and so it does.

But the print() function also returns its argument, but invisibly (like assignments, for example), otherwise you'd have the value printed twice in some cases. When the yes expression is evaluated, its result is what ifelse returns, and ifelse does not returns invisibly, so, it prints the result, since that was ran on the Global Environment and without any assignment.

We can test some variations and check what's going on.

> result <- print("true")
[1] "true" # Prints because the print() function was called.
> result
[1] "true" # The print function return its argument, which was assigned to the variable.

> ifelse(1<2, {print("true");"TRUE"},print("false"))
[1] "true" #This was printed because print() was called
[1] "TRUE" #This was printed because it was the value returned from the yes argument

If we assign this ifelse()

> result <- ifelse(1<2, {print("true");"TRUE"},print("false"))
[1] "true"
> result
[1] "TRUE"

We can also look at the case where both conditions conditions are evaluated:

> ifelse(c(1,3)<2, {print("true");"TRUE"},{print("false");"FALSE"})
[1] "true" # The yes argument prints this
[1] "false" # The no argument prints this
[1] "TRUE"  "FALSE" # This is the returned output from ifelse()

You should use ifelse to create a new object, not to perform actions based on a condition. For that, use if else. The R Inferno has good section (3.2) on the difference between the two.

Ry-
  • 218,210
  • 55
  • 464
  • 476
Molx
  • 6,816
  • 2
  • 31
  • 47
2

Just to add some arguments to the excellent answer of Molx. One of the reason why that happens relies on this:

length(print("true"))
[1] "true"
[1] 1
length(length(print("true")))
[1] "true"
[1] 1
length(length(length(print("true"))))
[1] "true"
[1] 1

... and so on...

In the R manual is stated that:

yes return values for true elements of test

note the values, and not value, here we have two values : one is the object of the print function (that has to print "true", most likely it is a call to a function, in this case print, that has as a result a character vector with true) and another one is the output print "true" which is the proper element of the ifelse statement.

It could be avoided with just:

 ifelse(1 < 2, "true" , "false" )
[1] "true"
SabDeM
  • 7,050
  • 2
  • 25
  • 38
  • Thank you very much for the down vote. As always never a comment follow a down vote. – SabDeM Jul 25 '15 at 23:48
  • This part is not correct: *here we have two values: one is the object of the print function (that has to print "true") and another one is to print "true".* What you have there are two different vectors, one element each, the second one is the **only** result from `ifelse()`. The first is only the result of `print()`, and is not part of the result of `ifelse()` at all. – Rich Scriven Jul 26 '15 at 03:55
  • @RichardScriven I have edited the question, now I think it is more clear. Our idea is the same maybe I did not write it clear. I know that now it is clear. – SabDeM Aug 01 '15 at 18:41
-1

Isn't it printing TRUE and returning TRUE at the same time?

So for example using cat instead of print would change the output...

This is basically related to ulfelders comment.

drmariod
  • 11,106
  • 16
  • 64
  • 110
  • mmm use `cat` will throw an error message and a warning because it interprets its argument; try `ifelse(1<2, cat("true"), cat("false"))`. – SabDeM Jul 25 '15 at 21:34