R - (Mathematical|Logical) Operators

> Procedural Languages > R

1 - Mathematical

# Addition
> 2+2
[1] 4
# Multiplication
> 2*3
[1] 6
# Division
> 4/2
[1] 2
# Power
> 2^3
[1] 8

2 - Logical

?base::logic
& boolean and
| boolean for
xor exactly or
! not
any any true
all all true
  • Not (!)
> !TRUE
[1] FALSE

Many operations in R are vectorized.

> x=1:4;y=6:3
> x
[1] 1 2 3 4
> y
[1] 6 5 4 3
# Logical
> x>2
[1] FALSE FALSE  TRUE  TRUE
> x>=2
[1] FALSE  TRUE  TRUE  TRUE
> y==1
[1] FALSE FALSE FALSE FALSE
Advertising

3 - Comparison

?Comparison
< Less than
> Greater than
== Equal to
Less than or equal to
>= Greater than or equal to
!= Not equal to
%in% Group membership
is.na Is NA
!is.na Is not NA

4 - Assignment

The symbol ← and = are the assignment operator.

x <- 1

is equivalent to:

x = 1

Proof:

> x<-1
> y=1
> x==y
[1] TRUE
Advertising

5 - Sequence

The : operator is used to create integer sequences.

> x=3:5
> x
[1] 3 4 5

6 - Subset

lang/r/operator.txt · Last modified: 2017/02/12 21:02 by gerardnico