What is the log() function in R?

Overview

The log() function in R is used to obtain the logarithm (the natural logarithm, by default) of a given input numeric or complex vector.

Syntax

log(x, base)
Syntax for the log() function

Parameter value

The log() function takes the following parameter values:

  • x: This is the numeric or complex vector whose logarithm is to be obtained.
  • base: This is a positive or complex number that represents the base at which the logarithm of x is to be computed.

Return value

The log(0) function returns the logarithmic value in the specified base passed to it.

Code

# creating a numeric vector
x = 100
# taking the logarithm of x in base 2
log(x, 2)
# taking the logarithm of x in base 10
log(x, 10)
# taking the natural logarithm of x
log(x)

Explanation

  • Line 2: We create a numeric vector x.
  • Line 5: We obtain the logarithm of x in base 2 using the log() function.
  • Line 8: We obtain the logarithm of x in base 10 using the log() function.
  • Line 11: We obtain the natural logarithm of x using the log() function.

Free Resources