Chapter 6 Basic Operations in R

6.1 Basic Commands

To get current working directory:-

getwd()
## [1] "C:/Users/mp64310/Desktop/100 Page R Book/bookdown-demo-master"

To set a new working directory use setwd()

List all the objects in your local workspace using ls() command. List all the files in your working directory using list.files() or dir().

We will start with the basic operations in R. Programming is best learnt hands on and hence I will strongly suggest to start typing following commands in your R Studio Console.

6.2 Airthmetic Operations in R

Addition, Substraction,multiplication and Division are some of the basic Airthmetic operations in R.

6.2.1 Addition & Substraction

2+2  # Addition
## [1] 4
2-2  # Substraction
## [1] 0

6.2.2 Multiplication & Division

2*2 # Multiplication
## [1] 4
2/2 # Division
## [1] 1

6.2.3 Squaring a number

2**2
## [1] 4

Another way to square a number is

3^2 # Square of three
## [1] 9

6.2.4 Square Root

2^(1/2)
## [1] 1.414214

Alternatively you can use sqrt function

sqrt(2)
## [1] 1.414214

Here ^ are the power opertors It can be used to calculate a^ b , i.e. ‘a’ rasie to the power of ‘b’

6.2.5 Integer Division- Returns Integer after division

5%/%2
## [1] 2

6.2.6 Modulus- Returns Remainder after division

5%%2
## [1] 1

6.2.7 Operators Precedence and Associativity

We can also see the operator associativity in R Associativity means which of the operations wil take precedence. Lets say you have both division and multiplication then which operation should take precdence.

#Should the result be 21 or 11?
5+2*3 
## [1] 11

As you might have guessed from the operator here the multiplication operator “*" takes precedence over “+” addition operator.

# What if we include brackets? What will be the result then?
(5+2)*3
## [1] 21
# What about Division "/" , Addition and Multiplication "*" operator.
# Check the result one.
5*5/2+3 
## [1] 15.5

6.3 Relational Operators in R

Relational operators are frequently used for conditional filtering of Data in R. We can assign a value to a variable using “<-”. A variable is named unit of data that can be assigned a value

x=5 # we have assigned x value of 5
y=7  # we have assigned y value of 7
# To check value of X just type x in console
x
## [1] 5
y
## [1] 7
#The '>' operator is caled relational operation and mean greater than
x>y
## [1] FALSE
y>x
## [1] TRUE

As you can see the output is logical .

5>=5 # Left Value Greater than or Equal to RightValue. IF TRUE the output will be a logical - TRUE . 
## [1] TRUE
5>=7 #IF FALSE output will be logical FALSE
## [1] FALSE
10==5 # Compare two values whether they are equal or not. IF Equal the output will be equal.
## [1] FALSE
5==5
## [1] TRUE
10!=5 # Similarly "!=" is used for notequal to . If the values are not equal it will give output TRUE
## [1] TRUE

6.4 Logical Operations in R

A logical operation will return an output of True or False and is frequently used for conditional filtering and other operations in R.

#Character "!" is Logical NOT
# so a not TRUE will evaluate to FALSE
!TRUE
## [1] FALSE
# Symbol "&" is used for elementwise Logical AND for example TRUE & TRUE will evaluate to TRUE, while TRUE and FALSE will evaluate to FALSE
TRUE & TRUE
## [1] TRUE
TRUE & FALSE
## [1] FALSE
# Similarly
FALSE & FALSE 
## [1] FALSE

Symbol “|” is used for Logical Operations If any of the condition is TRUE then TRUE ELSE FALSE

TRUE | TRUE # TRUE OR TRUE will return TRUE
## [1] TRUE
TRUE | FALSE # TRUE OR FALSE will return TRUE
## [1] TRUE
FALSE | FALSE # FALSE OR FALSE Will return FALSE
## [1] FALSE

The ‘&’ and ‘|’ symbol perform logical operations elementwise.

TRUE && TRUE
## [1] TRUE
TRUE & FALSE
## [1] FALSE
TRUE & FALSE
## [1] FALSE
FALSE && FALSE
## [1] FALSE

In the later chapters we will cover some of these operations in Vectors, Matrices and DataFrame.