Important Operators

Comparison operators

  • == (equal)
  • != (not equal)
  • > (greater than)
  • >= (greater than or equal)
  • < (less than)
  • <= (less than or equal)

Logical operators

  • & (and)
  • | (or)
  • ! (not)

Conditional Executions: if Statements

An if statement operates on length-one logical vectors.

Syntax

if(TRUE) { 
	statements_1 
} else { 
	statements_2 
}

Example

if(1==0) { 
	print(1) 
} else { 
	print(2) 
}
## [1] 2

Conditional Executions: ifelse Statements

The ifelse statement operates on vectors.

Syntax

ifelse(test, true_value, false_value)

Example

x <- 1:10 
ifelse(x<5, x, 0)
##  [1] 1 2 3 4 0 0 0 0 0 0
Jump to: next_page