Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if-else vs ifelse with lists

Tags:

r

if-statement

Why do the if-else construct and the function ifelse() behave differently?

mylist <- list(list(a=1, b=2), list(x=10, y=20))

l1 <- ifelse(sum(sapply(mylist, class) != "list")==0, mylist, list(mylist))

l2 <-
if(sum(sapply(mylist, class) != "list") == 0){  # T: all list elements are lists
  mylist
} else {
  list(mylist)
}

all.equal(l1,l2)
#  [1] "Length mismatch: comparison on first 1 components"
like image 504
Ryogi Avatar asked Feb 25 '12 23:02

Ryogi


People also ask

What is the difference between if and Ifelse () in R?

if statement: specifies a block of statements if the condition is true. elif statement: specifies a new condition to test, if the first condition is false. else statement: specifies a block of statements if the condition is false.

How do you check if a value is in a list in R?

To check if specific item is present in a given list in R language, use %in% operator. %in% operator returns TRUE if the item is present in the given list, or FALSE if not.

Should I use if or else if?

Use two if statements if both if statement conditions could be true at the same time. In this example, both conditions can be true. You can pass and do great at the same time. Use an if/else statement if the two conditions are mutually exclusive meaning if one condition is true the other condition must be false.

Does R have if-else?

To generalize, if-else in R needs three arguments: A statement (e.g. comparison operator) that evaluates to TRUE or FALSE. The value that R should return if the comparison operator is TRUE. The value that R should return if the comparison operator is FALSE.


2 Answers

From the ifelse documentation:

 ‘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’.

So your input has length one so the output is truncated to length 1.

You can also see this illustrated with a more simple example:

ifelse(TRUE, c(1, 3), 7)
# [1] 1
like image 135
Dason Avatar answered Nov 15 '22 05:11

Dason


if ( cond) { yes } else { no } is a control structure. It was designed to effect programming forks rather than to process a sequence. I think many people come from SPSS or SAS whose authors chose "IF" to implement conditional assignment within their DATA or TRANSFORM functions and so they expect R to behave the same. SA and SPSS both have implicit FOR-loops in there Data steps. Whereas R came from a programming tradition. R's implicit for-loops are built in to the many vectorized functions (including ifelse). The lapply/sapply fucntions are the more Rsavvy way to implement most sequential processing, although they don't succeed at doing lagged variable access, especially if there are any randomizing features whose "effects" get cumulatively handled.

ifelse takes an expression that builds a vector of logical values as its first argument. The second and third arguments need to be vectors of equal length and either the first of them or the second gets chosen. This is similar to the SPSS/SAS IF commands which have an implicit by-row mode of operation.

like image 25
IRTFM Avatar answered Nov 15 '22 04:11

IRTFM