Algebraic and logic operations in R

Algebraic and logic operations in R

Introduction

Table of contents

No heading

No headings in the article.

In R and every other programming language, there are three different kinds of operators. Arithmetic operators (+,-), relational set operators, and logic operators are examples of basic operators.

Arithmetic operator: As we previously discussed, a symbolic value can be converted to a numerical value. For example, if we print out the value of an object, such as x, it will appear as

x = 10

print(x)

Output:

10

Here we have assigned the value 10 to variable x

There are built-in functions in R that, when given a numerical number as an input, we know will act on the system to produce the value we want as an output.

Let's say we want to figure out the natural log value of 100, where the base is 10 and R has an in-built function for calculating the log

result <- log(100, base = 10)

print(result)

Output :

2

The result is 2 since the log of 100 in base 10 should equal 2 as we know that 10 to the power of 2 is equal to 100

R developers created a function that supports exponential functions. Let us find the exponential of 2

result <- exp(0.6931472)

print(result)

Output :

2

We are just reversing the function, there are lots of functions, but these are just examples of arithmetic operators.

Relational Operators: R has relational operators that make it easier to create relationships between two variables.

Operator

Description

        <

Less than

        >

Greater than

      ==

Equal to

      <=

Less than or Equal to

      >=

Greater than or equal to

      !=

Not Equal to

Let us take two Variables x and y, and build a relation between them,

x <- 10; y <- 20

print(x==y)

Output:

FALSE

R is telling it is false, which is true.

Logical Operators: Logical operators are operators that are used to combine or modify logical values (TRUE or FALSE) to create more complex logical expressions.

Operator

Description

        &

Element-wise

        I

Element-wise

        !

logical NOT

          A

          B

        A&B

      TRUE

      TRUE

      TRUE

      TRUE

      FALSE

      FALSE

      FALSE

      TRUE

      FALSE

      FALSE

      FALSE

      FALSE

Let's take the previous example and say,

x <- 10 y <- 13 x > y & x == y

output:

FALSE

Here, is how the logical operator & works as both of the statements x>y and x==y are false.

Conclusion: operators are symbols or keywords that perform operations on one or more values or variables. They are an essential part of any programming language and are used to manipulate data and control the flow of programs.

Â