0

Can someone please explain me what I am doing wrong. I don't understand the behaviour of the ifelse function in R. I expected that the ifelse function would return the whole list ids_match. However, these are the results I get with RStudio Version 1.3.1093 in the Console Window:

 cond1 = FALSE
 cond2 = FALSE
 cond3 = TRUE
 ids_match = list(1, 2, 3)

ifelse(cond1 & cond2 & cond3, ids_match[1], ids_match)

[[1]] [1] 1

ifelse(TRUE, ids_match[1], ids_match)

[[1]] [1] 1

ifelse(FALSE, ids_match[1], ids_match)

[[1]] [1] 1

ifelse(FALSE, "TRUE", "FALSE")

[1] "FALSE"

ifelse(TRUE, "TRUE", "FALSE")

[1] "TRUE"`

Emil
  • 85
  • 5

1 Answers1

0

According to ?ifelse:

Usage: ifelse(test, yes, no)

Description: ‘ifelse’ returns a value with the same shape as ‘test’ which is filled with elements selected from either ‘yes’ or ‘no’ depending on whether the element of ‘test’ is ‘TRUE’ or ‘FALSE’.

Now, since (cond1 & cond2 & cond3) is a single boolean variable (i.e. length(cond1 & cond2 & cond3) == 1), your response will also have length of 1.

Also see related discussion here.

Otto Kässi
  • 2,943
  • 1
  • 10
  • 27
  • glad to have helped! if you think my reply sufficiently answered your question, could I ask you to approve it? thanks! – Otto Kässi Jan 28 '21 at 12:09