R Cheatsheet: Logical Operators


#Equal to

==

#Non equal to

!=

#Less than

<

#Less than or equal to

<=

#Greater than

<

#Greater than or equal to

<=

#Negation (NOT operator), or to negate a boolean expression:

!(b==c)

#AND (Conjunction)

#we can use both ‘&’ and ‘&&’ the difference between this two symbols is that the first evaluates ALL the elements of a vector, and the second only the first element of a vector.

and_operator

#OR (Disjunction)

#The OR operator follows a similar set of rules. The `|` version of OR evaluates OR across an entire vector, while the `||` version of OR only evaluates the first member of a vector.

or_operator

#Important!: The rules of precedence dictate that ALL the AND operations are evaluated first, then the OR operations are calculated later.

FALSE && 6 >= 6 || 7 >= 8 || 50 <= 49.5

#The expression from above results in FALSE, because by the rules of precedence we first evaluate FALSE && 6 >= 6  OBTAINING  a FALSE, after thaht we evaluate the OR operations resulting in FALSE.

#R has a function to evaluate is an expression is TRUE

isTRUE()

isTRUE

#Identical is a function which evaluates if two elements are equal returning TRUE.

identical

#XOR

#The xor() function stands for exclusive OR. If one argument evaluates to TRUE and one argument evaluates to  FALSE, then this function will return TRUE, otherwise it will return FALSE.

xor(5==6, !FALSE)
[1] TRUE

#WHICH

#A function that returns the elements of a vector complying with a logical operation:

x<-sample(10)

WHICH(x>5)

#ANY

#Returns TRUE if at least an element of the vector evaluated is TRUE.

ANY(x>2)

#ALL

#Returns TRUE if all elements are true:

ALL(x>0)

Acerca de martincx

Fútbol, cine, historia y literatura. Quejándome del fútbol moderno. Ojalá y escribir fuera un trabajo remunerado.

Este blog se enriquece con los comentarios, deja acá tu impresión.

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Salir /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Salir /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Salir /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Salir /  Cambiar )

Conectando a %s

A %d blogueros les gusta esto: