R Operators - r - learn r - r programming



 r operator
  • R has many operators to carry out different mathematical and logical operations.
  • Operators in R can mainly be classified into the following categories.

R Arithmetic Operators

  • These operators are used to carry out mathematical operations like addition and multiplication. Here is a list of arithmetic operators available in R.
 arithmetic operator

Arithmetic Operators in R

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
^ Exponent
%% Modulus (Remainder from division)
%/% Integer Division

Example

> x <- 5
> y <- 16

>x+y
[1] 21

> x-y
[1] -11

> x*y
[1] 80

> y/x
[1] 3.2

> y%/%x
[1] 3

> y%%x
[1] 1

>y^x
[1] 1048576
r programming operators

Types of operators

R Relational Operators

  • Relational operators are used to compare between values. Here is a list of relational operators available in R.
Operator Description
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to

Example:

> x <- 5
> y <- 16

> x<y
[1] TRUE

> x>y
[1] FALSE

> x<=5
[1] TRUE

> y>=20
[1] FALSE

> y == 16
[1] TRUE

>x != 5
[1] FALSE

Operation on Vectors

  • The above mentioned operators work on vectors. The variables used above were in fact single element vectors.
  • We can use the function c() (as in concatenate) to make vectors in R.
  • All operations are carried out in element-wise fashion.

Here is an example.

> x <- c(2,8,3)
> y <- c(6,4,1)

>x+y
[1]  8124

> x>y
[1] FALSE  TRUETRUE
  • When there is a mismatch in length (number of elements) of operand vectors, the elements in shorter one is recycled in a cyclic manner to match the length of the longer one.
  • R will issue a warning if the length of the longer vector is not an integral multiple of the shorter vector.
> x <- c(2,1,8,3)
> y <- c(9,4)

>x+y# Element of y is recycled to 9,4,9,4
[1] 115177

> x-1# Scalar 1 is recycled to 1,1,1,1
[1] 1072

>x+c(1,2,3)
[1]  33114
Warning message:
In x + c(1, 2, 3) :
 longer object length isnot a multiple of shorter object length

R Logical Operators

  • Logical operators are used to carry out Boolean operations like AND, OR etc.
Operator Description
! Logical NOT
& Element-wise logical AND
&& Logical AND
| Element-wise logical OR Element-wise logical OR
|| Logical OR Logical OR
  • Operators & and | perform element-wise operation producing result having length of the longer operand.
  • But && and || examines only the first element of the operands resulting into a single length logical vector.
  • Zero is considered FALSE and non-zero numbers are taken as TRUE.

Example:

> x <- c(TRUE,FALSE,0,6)
> y <- c(FALSE,TRUE,FALSE,TRUE)

> !x
[1] FALSE  TRUETRUE FALSE

>x&y
[1] FALSE FALSEFALSE  TRUE

> x&&y
[1] FALSE

>x|y
[1]  TRUETRUE FALSE  TRUE

> x||y
[1] TRUE

R Assignment Operators

  • These operators are used to assign values to variables.
Operator Description
<-, <<-, = Leftwards assignment
->, ->> Rightwards assignment
  • The operators <- and = can be used, almost interchangeably, to assign to variable in the same environment.
  • The <<- operator is used for assigning to variables in the parent environments (more like global assignments).
  • The rightward assignments, although available are rarely used.
> x <- 5
> x
[1] 5

> x = 9
> x
[1] 9

>10 -> x
> x
[1] 10



Related Searches to R Operators