Logarithmic and Power Functions in R Programming

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.
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 = bxthen 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 
 log(10) = 2.302585 
[log10(number)] function returns the common logarithm i.e. base 10. log10(10) := 1 
 log10(10) := 1 
[log2(number)] returns the binary logarithm i.e. base 2. log2(10) := 3.321928 
 log2(10) := 3.321928 
[log(number, b)] return the logarithm with base b. log(10, 3) := 2.095903 
 log(10, 3) := 2.095903 
[log1p(number)] returns log(1+number) for number << 1 precisely. log1p(10) := 2.397895 
 log1p(10) := 2.397895 
[exp(number)] returns the exponential. exp(10) := 22026.47 
 exp(10) := 22026.47 
[expm1(number)] returns the exp(number)-1 for number <<1 precisely. expm1(10) := 22025.47 
 expm1(10) := 22025.47 
Example:
# R program to illustrate use of log functions  x <- 10base <- 3  # Computes natural logarithmlog(x)  # Computes common logarithmlog10(x)  # Computes binary logarithmlog2(x)  # Computes logarithm of # x with base blog(x, base)  # Computes accurately# log(1+x) for x<<1log1p(x)  # Computes exponentialexp(x)  # Computes accurately # exp(x)-1 for x<<1expm1(x)
Output:
[1] 2.302585
[1] 1
[1] 3.321928
[1] 2.095903
[1] 2.397895
[1] 22026.47
[1] 22025.47

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 
# R program to illustrate # the use of Power Functionx <- 10y <- 3  # 1st Method`^`(x, y)  # 2nd Methodx^y  # 3rd Methodx**y
Output:
[1] 1000
[1] 1000
[1] 1000

Picked
R-Functions
R-Mathematics
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
R - if statement
How to filter R DataFrame by values in a column?
Logistic Regression in R Programming
Replace Specific Characters in String in R
How to import an Excel File into R ?
Joining of Dataframes in R Programming