Skip to content
Related Articles

Related Articles

Improve Article

Logarithmic and Power Functions in R Programming

  • Last Updated : 01 Jun, 2020
Geek Week

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:




# R program to illustrate use of log functions
  
x <- 10
base <- 3
  
# Computes natural logarithm
log(x)
  
# Computes common logarithm
log10(x)
  
# Computes binary logarithm
log2(x)
  
# Computes logarithm of 
# x with base b
log(x, base)
  
# Computes accurately
# log(1+x) for x<<1
log1p(x)
  
# Computes exponential
exp(x)
  
# Computes accurately 
# exp(x)-1 for x<<1
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 




# R program to illustrate 
# the use of Power Function
x <- 10
y <- 3
  
# 1st Method
`^`(x, y)
  
# 2nd Method
x^y
  
# 3rd Method
x**y

Output:

[1] 1000
[1] 1000
[1] 1000



My Personal Notes arrow_drop_up
Recommended Articles
Page :