Logarithmic and Power Functions in R Programming
Last Updated :
01 Jun, 2020
Logarithm and Power are two very important mathematical functions that help in the calculation of data that is growing exponentially with time.
First is the Logarithm, to which the general way to calculate the logarithm of the value in the base is with the log()
function which takes two arguments as value and base, by default it computes the natural logarithm and there are shortcuts for common and binary logarithm i.e. base 10 and 2. Value can be number or vector.
Second is the Power, to calculate a base number raised to the power of exponent number. In this article, there are three methods shown to calculate the same i.e. baseexponent.
Log Function in R
It is the inverse of the exponential function, where it represents the quantity that is the power to the fixed number(base) raised to give the given number. It returns the double value.
Formula:
If y = bx
then logby = x
Example:
if 100 = 102
then log10100 = 2
List of various log()
functions:
The number is numeric or complex vector and the base is a positive or complex vector with the default value set to exp(1).
- The log function [log(number)] in R returns the natural logarithm i.e. base e.
log(10) = 2.302585
- [log10(number)] function returns the common logarithm i.e. base 10.
log10(10) := 1
- [log2(number)] returns the binary logarithm i.e. base 2.
log2(10) := 3.321928
- [log(number, b)] return the logarithm with base b.
log(10, 3) := 2.095903
- [log1p(number)] returns log(1+number) for number << 1 precisely.
log1p(10) := 2.397895
- [exp(number)] returns the exponential.
exp(10) := 22026.47
- [expm1(number)] returns the exp(number)-1 for number <<1 precisely.
expm1(10) := 22025.47
Example:
x < - 10
base < - 3
log(x)
log10(x)
log2(x)
log(x, base)
log1p(x)
exp(x)
expm1(x)
|
Output:
[1] 2.302585
[1] 1
[1] 3.321928
[1] 2.095903
[1] 2.397895
[1] 22026.47
[1] 22025.47
Power Function
If there two numbers base and exponent, it finds x raised to the power of y i.e. xy.
It returns double value. It needs two arguments:
x = floating point base value
y = floating point power value
Example :
103 = 10*10*10 = 1000
x < - 10
y < - 3
`^`(x, y)
x^y
x * * y
|
Output:
[1] 1000
[1] 1000
[1] 1000
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...