This post is gonna be pretty simple and deals with the basic mathematical operations in R programming before we go into other concepts. Some of the built-in functions are highly helpful as you move along. The four basic mathematical operations which are addition, subtraction, multiplication and division are pretty straightforward in R and easier to understand.
a <- 2+3
a
b <- 5-4
b
c <- 4*5
c
d <- 10/4
d
We will also get the output as expected:
Next is the exponential which can be done by using '^' symbol (for example: 2^4 which is equal to 16). Another is the modulus operator. In R double percentage sign is used to perform modulus operation (for example: 14%%5 which gives us result as 4).
Down here we will take a look at some built-in R functions used for mathematical purposes.
e <- 2^4
e
f <- 14%%5
f
sqrt(81)
abs(-21)
g <- 20/3
g
round(g, digits=2)
The sqrt() function calculates the square root of a number. The abs() function returns the absolute value of a number. The round() function returns the rounded value of a number. Parameters digits is used to specify the number of decimal points that you want your number to be rounded off.
The output is as follows:
Likewise there are many built-in math's functions like finding trigonometrical values, logarithmic values, generating and searching a pattern in number and so on in R.