## Search this blog

Showing posts with label Logical Operators. Show all posts
Showing posts with label Logical Operators. Show all posts

## Saturday 11 August 2018

### R Operators – Arithmetic Operators, Relational Operators, Logical Operators

In programming, an operator is a symbol that is used to perform any specific mathematics or logical operation. In simple language, we can say that the operator is used to perform some operations and like other programming languages R has a set of various types of R operators that can be divided into the following categories.

• Arithmetic Operator
• Relational Operator
• Logical Operator
• Assignment Operator

## Arithmetic Operators in R

Arithmetic operators are used for performing mathematical operations like addition, subtraction etc.

OperatorDescription
-Subtraction (To Subtract one variable from another)
*Multiplication (To Multiply variables)
/Division (To divide the first variable with the second)
^Exponent (The first variable's value raised to the exponent of second value)
%%Modulus (Gives the remainder of the first variable's value with the second)

See an example here

```> # An addition
> 5 + 10
[1] 15
>
> # A subtraction
> 10 - 5
[1] 5
>
> # A multiplication
> 5 * 15
[1] 75
>
> # A division
> 12 / 2
[1] 6
>
> # Exponentiation
> 2^6
[1] 64
>
> # Modulus
> 25%%4
[1] 1
>
```

## Relational Operators in R

Relational Operators are used for comparing values and produce a boolean result.

OperatorDescription
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
==Equal to
!=Not equal to

See an example here

```> # Greater than
> a <- 10
> b <-20
> print(b>a)
[1] TRUE
>
> # Less than
> a <- 10
> b <-20
> print(b<a)
[1] FALSE
>
> # Greater than or equal to
> a <- c(10,20,5)
> b <- c(20,10,5)
> print(b>=a)
[1] TRUE FALSE TRUE
>
> # Less than or equal to
> a <- c(10,20,5)
> b <- c(20,10,5)
> print(b<=a)
[1] FALSE TRUE TRUE
>
> # Equal to
> a <- 10
> b <-10
> print(b==a)
[1] TRUE
>
> # Not equal to
> a <- 10
> b <-10
> print(b!=a)
[1] FALSE
```

## Logical Operators in R

The logical operators are used to carry out boolean operations between variables. Number zero (0) is considered FALSE and all numbers greater than 0 are considered TRUE.

OperatorDescription
&Element wise logical AND (It checks each value of both vector and gives output TRUE if both values are TRUE)
|Element wise logical OR (It checks each value of both vector and gives output TRUE if any one value is TRUE)
!Logical NOT (It takes each value of vector and reverse it's logical value)
&&Logical AND (It checks only first value of both vector and gives output TRUE if both values are TRUE)
||Logical OR (It checks only first value of both vector and gives output TRUE if any of them are TRUE)

See an example here

```> #Take two vectors
> a <- c(0,20,30,0,FALSE)
> b <- c(12,23,42,0,TRUE)
>
> #Print Element wise logical AND
> print(a&b)
[1] FALSE  TRUE  TRUE FALSE FALSE
>
> #Print Element wise logical OR
> print(a|b)
[1]  TRUE  TRUE  TRUE FALSE  TRUE
>
> #Print Logical AND
> print(a&&b)
[1] FALSE
>
> #Print Logical OR
> print(a||b)
[1] TRUE
>
> #Print Logical NOT
> print(!a)
[1]  TRUE FALSE FALSE  TRUE  TRUE
> print(!b)
[1] FALSE FALSE FALSE  TRUE FALSE
>
```
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "http://www.awasthiashish.com/search/label/Logical%20Operators", "fetch_time": 1725722376000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2024-38/segments/1725700650883.10/warc/CC-MAIN-20240907131200-20240907161200-00546.warc.gz", "warc_record_offset": 36044054, "warc_record_length": 29645, "token_count": 923, "char_count": 3246, "score": 3.84375, "int_score": 4, "crawl": "CC-MAIN-2024-38", "snapshot_type": "latest", "language": "en", "language_score": 0.7596376538276672, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00059-of-00064.parquet"}